Why I can not change the column name in DataFrame? I tried the following formula in python. Can anyone help me?
df1 = df1.rename(columns={'2019-04-01 00:00:00': 'xx'})
2019-04-01 00:00:00
c
f
g
Based on my comment.
The column might be in datetime
format, not string
.
df1 = df1.rename(columns={datetime.strptime('2019-04-01 00:00:00', '%Y-%m-%d %H:%M:%S'): 'xx'})
You can also assign a new name to a column or mulitple columns as follows:
df.columns = ['xx']
or if there are multiple columns
df.columns = ['xx', 'yy']
The column name is not the exact same thing in this instance so when it is searching for '2019-04-01 00:00:00' it is not found and thus not changed. This could be because one is a string and the other is DateTime. Either make sure you have the type correct or try another method to rename the columns like so:
df1.columns = ['xx']
df1
This will require you to rename all columns so fixing the type is likely the way to go.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.