【问题标题】:Pandas Group By And Get DummiesPandas Group By 和 Get Dummies
【发布时间】:2020-12-17 13:35:24
【问题描述】:

我想让每个唯一值获取虚拟变量。想法是将数据框变成多标签目标。我该怎么做?

数据:

           ID                      L2
           A                 Firewall
           A                 Security
           B           Communications
           C                 Business
           C                 Switches

所需的输出:

ID   Firewall  Security  Communications  Business   Switches
 A      1          1             0              0         0
 B      0          0             1              0         0
 C      0          0             0              1         1

我试过pd.pivot_table,但它需要一个列来聚合。我也尝试过对this link 的回答,但它对值求和,而不是仅仅变成二进制虚拟列。非常感谢您的帮助。非常感谢!

【问题讨论】:

    标签: python-3.x pandas pandas-groupby pivot-table dummy-variable


    【解决方案1】:

    让我们set_index 然后get_dummies,因为我们在每个 ID 中有多个重复,我们需要 sumlevel = 0

    s = df.set_index('ID')['L2'].str.get_dummies().max(level=0).reset_index()
    Out[175]: 
      ID  Business  Communications  Firewall  Security  Switches
    0  A         0               0         1         1         0
    1  B         0               1         0         0         0
    2  C         1               0         0         0         1
    

    【讨论】:

    • 感谢您的回答,但我有一个更大的数据框,使用 sum 实际上是 summing 的值,所以我不只是得到二进制列。
    • @KrishnangKDalal 使用max 而不是sum
    • 好了,就是!!非常感谢。另外,谢谢大家的帮助
    • 感谢解答,为什么要使用max函数?
    • @Utopia 具有 0 和 1 的多个相同列我们只想返回 1 其中任何一个都是 1 ,这就是为什么使用 max
    【解决方案2】:

    crosstab,然后转换为布尔值:

    pd.crosstab(df['ID'],df['L2']).astype(bool)
    

    输出:

    L2  Business  Communications  Firewall  Security  Switches
    ID                                                        
    A      False           False      True      True     False
    B      False            True     False     False     False
    C       True           False     False     False      True
    

    【讨论】:

      【解决方案3】:

      如果您更改aggfunc=any,则可以使用pivot_table

      print(df.pivot_table(index='ID', columns='L2', 
                           aggfunc=any, fill_value=False)\
              .astype(int))
      L2  Business  Communications  Firewall  Security  Switches
      ID                                                        
      A          0               0         1         1         0
      B          0               1         0         0         0
      C          1               0         0         0         1
      

      也许reset_index 在最后将 ID 作为列

      【讨论】:

        【解决方案4】:

        你可以试试这个:

        df1 = pd.read_csv("file.csv")
        df2 = df1.groupby(['ID'])['L2'].apply(','.join).reset_index()
        df3 = df2["L2"].str.get_dummies(",")
        df = pd.concat([df2, df3], axis = 1)
        print(df)
        

        输出:

          ID                 L2  Business  Communications  Firewall  Security  Switches
        0  A  Firewall,Security         0               0         1         1         0
        1  B     Communications         0               1         0         0         0
        2  C  Business,Switches         1               0         0         0         1
        

        备选方案:

        df = df.groupby(['ID'])['L2'].apply(','.join).str.get_dummies(",").reset_index()
        print(df)
        

        【讨论】:

          猜你喜欢
          • 2018-03-05
          • 1970-01-01
          • 2023-01-15
          • 2016-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-06
          相关资源
          最近更新 更多