【问题标题】:How do I rename the 1 values in a categorical variable as the header of the column?如何将分类变量中的 1 值重命名为列的标题?
【发布时间】:2018-11-16 20:00:09
【问题描述】:

我有一个带有多个分类变量(1 或 0)的 Pandas DataFrame,我想根据自己的变量名重命名 1 值,该怎么做?

          Name           Achiever  Arranger  Belief  Consistency  Deliberative  
0          Alex           1.0       0.0     0.0          0.0           0.0   
13         Luke           0.0       0.0     0.0          1.0           0.0     

我想退货

          Name           Achiever  Arranger  Belief  Consistency  Deliberative  
0          Alex        Achiever      0.0     0.0          0.0           0.0   
13         Luke           0.0       0.0     0.0    Conistency          0.0 

【问题讨论】:

标签: python pandas header rename


【解决方案1】:

你可以试试这个:

for col in df.columns.values:
    df[col].replace(to_replace=1, value=col, inplace=True)

【讨论】:

    猜你喜欢
    • 2018-05-20
    • 2019-04-29
    • 2017-05-24
    • 2018-01-10
    • 2018-01-10
    • 1970-01-01
    • 2017-04-03
    • 2022-01-01
    • 2021-07-18
    相关资源
    最近更新 更多