Reputation: 1938
I have a dataframe with several columns, one of them is co
. I can rename this column with rename()
but if I select the co
column and create a new dataframe with it, then it loses it's name and I'm unable to assign a new column name. How could I do that?
Works:
data = data.rename(columns={'co':'BCH/USDT'})
print(data)
id market BCH/USDT vo
dt
2020-01-19 00:00:00+00:00 1910 BCH/USDT 338.79 305020.88
2020-01-18 00:00:00+00:00 256 BCH/USDT 367.98 251207.19
2020-01-17 00:00:00+00:00 255 BCH/USDT 326.58 395293.42
2020-01-16 00:00:00+00:00 254 BCH/USDT 340.00 247311.30
2020-01-15 00:00:00+00:00 253 BCH/USDT 348.35 498653.96
Rename doesn't works:
df = data['co']
df.columns = ['BCH/USDT']
print(df)
dt
2020-01-19 00:00:00+00:00 338.79
2020-01-18 00:00:00+00:00 367.98
2020-01-17 00:00:00+00:00 326.58
2020-01-16 00:00:00+00:00 340.00
2020-01-15 00:00:00+00:00 348.35
Same problem with:
df.rename(columns={'':'BCH/USDT'}, inplace = True)
Upvotes: 2
Views: 1298
Reputation: 17152
you can name your pd.Series
if you want
df.name = 'BCH/USDT'
this name will be kept if you join/merge this pd.Series
with another pd.DataFrame
and the column name will be the series name
or
you could transform a pd.Series
to 1 column pd.DataFrame
:
df = df.to_frame('BCH/USDT')
Upvotes: 2
Reputation: 323326
You are slice a series
not sub dataframe
, adding []
df = data[['co']]
Upvotes: 4