Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Dataframe column names julia v1.0

In 0.6 I was using:

colnames = ["Date_Time","Date_index","Time_index"]
names!(data1_date_time_index.colindex, map(parse, colnames))

What is the syntax for v1.0 - right now .colindex is not found.

Per DataFrames docs:

rename!(data1_date_time_index, f => t for (f, t) =
   zip([:x1, :x1_1, :x1_2],     
       [:Date_Time, :Date_index, :Time_index]))
like image 582
Andrew Bannerman Avatar asked Aug 17 '18 20:08

Andrew Bannerman


People also ask

How do I change the column name in Julia DataFrame?

Additionally, in your example, you should use select! in order to modify the column names in place, or alternatively do 'df = select(df, "col1" => "Id", "col2" => "Name")` as select always return a new DataFrame .

Can we rename column name in DataFrame?

One way of renaming the columns in a Pandas Dataframe is by using the rename() function. This method is quite useful when we need to rename some selected columns because we need to specify information only for the columns which are to be renamed. Example 1: Rename a single column.

How do I rename column names?

To change a column name, enter the following statement in your MySQL shell: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Replace table_name , old_column_name , and new_column_name with your table and column names.

How do I rename a column in a DataFrame list?

Rename Columns with List using set_axis() Alternatively, you can use DataFrame. set_axis() method to rename columns with list. use inplace=True param to rename columns on the existing DataFrame object.


3 Answers

Assuming data1_date_time_index is a DataFrame that has three columns use:

colnames = ["Date_Time","Date_index","Time_index"]
names!(data1_date_time_index, Symbol.(colnames))

I am not 100% sure if this is what you want, as your example was not fully reproducible (so if actually you needed something else can you please submit full code that can be run).

The problem with data1_date_time_index.colindex is that currently . is used to access columns of a DataFrame by their name (and not fields of DataFrame type). In general you are not recommended to use colindex as it is not part of exposed API and might change in the future. If you really need to reach it use getfield(data_frame_name, :colindex).

EDIT

In DataFrames 0.20 you should write:

rename!(data1_date_time_index, Symbol.(colnames))

and in DataFrames 0.21 (which will be released before summer 2020) also passing strings directly will most probably be allowed like this:

rename!(data1_date_time_index, colnames)

(see here for a related discussion)

like image 176
Bogumił Kamiński Avatar answered Oct 11 '22 08:10

Bogumił Kamiński


You can rename column through select also

For Ex:

df = DataFrame(col1 = 1:4, col2 = ["John", "James", "Finch", "May"])

│ Row │ col1  │ col2   │
│     │ Int64 │ String │
├─────┼───────┼────────┤
│ 1   │ 1     │ John   │
│ 2   │ 2     │ James  │
│ 3   │ 3     │ Finch  │
│ 4   │ 4     │ May    │


select(df, "col1" => "Id", "col2" => "Name")

│ Row │ Id    │ Name   │
│     │ Int64 │ String │
├─────┼───────┼────────┤
│ 1   │ 1     │ John   │
│ 2   │ 2     │ James  │
│ 3   │ 3     │ Finch  │
│ 4   │ 4     │ May    │
like image 21
Mohamed Thasin ah Avatar answered Oct 11 '22 08:10

Mohamed Thasin ah


Rename columns:

names!(df, [:c1,:c2,:c3]) #(all)

rename!(df, Dict(:oldCol => :newCol)) # (a selection)

(from: https://syl1.gitbook.io/julia-language-a-concise-tutorial/useful-packages/dataframes )

like image 3
Antonello Avatar answered Oct 11 '22 08:10

Antonello