有没有办法将R中变量的名称更改为标签值?
例如,当变量表示“年龄带”时,我试图将其更改为“三向带状年龄组”:
Data_2017_18.ageband.value_counts()
Out[118]:
51 to 99 13320
30 to 40 10985
1 to 29 5002
Name: ageband, dtype: int64我试过了,但似乎没有用:
import pandas as pd
Data_2017_18['Three-way banded age group'] = Data_2017_18['ageband'].astype("category")发布于 2020-08-24 14:58:14
注意:将列名重命名为包含空格的名称被认为是一种不良做法,应该使用下划线来避免。
要在熊猫中重命名列,只需使用rename方法即可。
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}')https://stackoverflow.com/questions/63563363
复制相似问题