Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming tuple column name in dataframe

I am new to python and pandas. I have attached a picture of a pandas dataframe, Pic I need to know how I can fetch data from the last column and how to rename the last column.

like image 424
SowD Avatar asked Apr 08 '17 04:04

SowD


People also ask

How to rename columns in a Dataframe?

The columns can also be renamed by directly assigning a list containing the new names to the columns attribute of the dataframe object for which we want to rename the columns. The disadvantage with this method is that we need to provide new names for all the columns even if want to rename only some of the columns. filter_none.

How to access column by name in a tuple?

# access column by name df ['vehicle_id_reservation_count`] This is a straight forward conversion on all columns that are named by a tuple: # rename columns def rename (col): if isinstance (col, tuple): col = '_'.join (str (c) for c in col) return col df.columns = map (rename, df.columns)

How to rename all the column names in Python DF1?

Rename all the column names in python: Below code will rename all the column names in sequential order # rename all the columns in python df1.columns = ['Customer_unique_id', 'Product_type', 'Province'] first column is renamed as ‘Customer_unique_id’. second column is renamed as ‘Product_type’. third column is renamed as ‘Province’.

How do I rename multiple columns in a table in Python?

You just need to separate the renaming of each column using a comma: So this is the full Python code to rename the columns: You’ll now get the correct column names:


2 Answers

fetch data from the last column

Retrieving the last column using df.iloc[:,-1] as suggested by other answers works fine only when it is indeed the last column.

However, using absolute column positions like -1 is not a stable solution, i.e. if you add some other column, your code will break.

A stable, generic approach

First of all, make sure all your column names are strings:

# rename columns
df.columns = [str(s) for s in df.columns]
# access column by name
df['(vehicle_id, reservation_count)']

rename the last column

It is preferable to have similar column names for all columns, without brackets in them - make your code more readable and your dataset easier to use:

# access column by name
df['vehicle_id_reservation_count`]

This is a straight forward conversion on all columns that are named by a tuple:

# rename columns
def rename(col):
    if isinstance(col, tuple):
        col = '_'.join(str(c) for c in col)
    return col
df.columns = map(rename, df.columns)
like image 161
miraculixx Avatar answered Oct 14 '22 12:10

miraculixx


You can use:

df = df.rename(columns = {df.columns[-1] : 'newname'})

Or:

df.columns = df.columns[:-1].tolist() + ['new_name']

It seems solution:

df.columns.values[-1] = 'newname'

is buggy. Because after rename pandas functions return weird errors.

For fetch data from last column is possible use select by position by iloc:

s = df.iloc[:,-1]

And after rename:

s1 = df['newname']
print (s1)

Sample:

df = pd.DataFrame({'R':[7,8,9],
                   'T':[1,3,5],
                   'E':[5,3,6],
                   ('Z', 'a'):[7,4,3]})

print (df)
   E  T  R  (Z, a)
0  5  1  7       7
1  3  3  8       4
2  6  5  9       3

s = df.iloc[:,-1]
print (s)
0    7
1    4
2    3
Name: (Z, a), dtype: int64

df.columns = df.columns[:-1].tolist() + ['new_name']
print (df)
   E  T  R  new_name
0  5  1  7         7
1  3  3  8         4
2  6  5  9         3

df = df.rename(columns = {('Z', 'a') : 'newname'})
print (df)
   E  T  R  newname
0  5  1  7        7
1  3  3  8        4
2  6  5  9        3
s = df['newname']
print (s)
0    7
1    4
2    3
Name: newname, dtype: int64
df.columns.values[-1] = 'newname'
s = df['newname']
print (s)
>KeyError: 'newname'
like image 39
jezrael Avatar answered Oct 14 '22 13:10

jezrael