Python 重命名Pandas数据框的一列并将其更改为';s型
有没有办法将R中变量的名称更改为标签值 例如,当变量表示“年龄段”时,我试图将其更改为“三向分带年龄组”: 我试过这个,但似乎不起作用:Python 重命名Pandas数据框的一列并将其更改为';s型,python,pandas,dataframe,Python,Pandas,Dataframe,有没有办法将R中变量的名称更改为标签值 例如,当变量表示“年龄段”时,我试图将其更改为“三向分带年龄组”: 我试过这个,但似乎不起作用: import pandas as pd Data_2017_18['Three-way banded age group'] = Data_2017_18['ageband'].astype("category") 注意:将列名重命名为包含空格的名称被认为是一种不好的做法,应该使用下划线来避免 要重命名pandas中的列,只需使用以下方
import pandas as pd
Data_2017_18['Three-way banded age group'] = Data_2017_18['ageband'].astype("category")
注意:将列名重命名为包含空格的名称被认为是一种不好的做法,应该使用下划线来避免 要重命名pandas中的列,只需使用以下方法
import pandas as pd
d = {'ageband': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
# Before : ageband col2
# 0 1 3
# 1 2 4
print(f'Before : {df}')
# Rename column name
df=df.rename(columns={'ageband' : "Three-way banded age group"})
# Convert type name to `category`
df[['Three-way banded age group']] = df[['Three-way banded age group']].astype('category')
# After : Three-way banded age group col2
# 0 1 3
# 1 2 4
print(f'After : {df}')
import pandas as pd
d = {'ageband': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
# Before : ageband col2
# 0 1 3
# 1 2 4
print(f'Before : {df}')
# Rename column name
df=df.rename(columns={'ageband' : "Three-way banded age group"})
# Convert type name to `category`
df[['Three-way banded age group']] = df[['Three-way banded age group']].astype('category')
# After : Three-way banded age group col2
# 0 1 3
# 1 2 4
print(f'After : {df}')