I am new to python and pandas. I have attached a picture of a pandas dataframe, I need to know how I can fetch data from the last column and how to rename the last column.
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.
# 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)
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’.
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:
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)
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With