Rの変数の名前をラベル値に変更する方法はありますか?
たとえば、変数に「ageband」と表示されている場合、それを「3方向バンド年齢グループ」に変更しようとしています。
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")
注:列名の名前をスペースを含む名前に変更することは悪い習慣と見なされ、代わりにアンダーソールを使用して回避する必要があります。
パンダの列の名前を変更するには、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}')
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加