【问题标题】:Is there a way of aggregating rows without summing up their results?有没有一种方法可以在不汇总结果的情况下聚合行?
【发布时间】:2019-09-27 07:15:21
【问题描述】:

我的 DataFrame 由 2 列组成。一个有病人的身份证,一个有病人的问题。 我需要创建一个 DataFrame,其中患者的所有问题都与相应的患者 ID 在一行中。目前,如果患者有问题,此数据框会创建一个唯一的行。

PAT_MRN_ID  Problem                      
9641956     Headache
9641956     Stomach_ache  
8227510     Headache 
8165474     Chicken_pox
7860000     Stomach_ache

上面的例子需要点赞:

 PAT_MRN_ID  Headache         Stomach_ache      Chicken_pox
 9641956      1                1                   0
 8227510      1                0                   0
 8165474      0                0                   1
 7860000      0                1                   0

最终我想将 DataFrame 归类为上述示例。我尝试使用循环和聚合,但不幸的是我的基本编程技能还不够。

【问题讨论】:

    标签: python pandas aggregate-functions pyodbc categorization


    【解决方案1】:

    使用 pd.get_dummies。

    import pandas as pd
    df = pd.DataFrame({"PAT_MRN_ID": [9641956, 9641956, 8227510, 8165474, 7860000], "Problem": ["Head", "Stomach", "Head", "Pox", "Stomach"]})
    pd.get_dummies(df,columns=["Problem"]).groupby(df.index).sum()
    
                      Problem_Head  Problem_Pox  Problem_Stomach
    PAT_MRN_ID                                            
    7860000                0            0                1
    8165474                0            1                0
    8227510                1            0                0
    9641956                1            0                1
    

    【讨论】:

      【解决方案2】:

      使用get_dummiesDataFrame.set_index,每个索引的最大值和DataFrame.reset_index

      df1 = (pd.get_dummies(df.set_index('PAT_MRN_ID')['Problem'], 
                          prefix='', prefix_sep='')
               .max(axis=0, level=0)
               .reset_index())
      print (df)
      
      PAT_MRN_ID Chicken_pox  Headache  Stomach_ache                                  
      9641956               0         1             1
      8227510               0         1             0
      8165474               1         0             0
      7860000               0         0             1
      

      【讨论】:

        【解决方案3】:

        先得到“问题”的假人,然后分组

        import pandas as pd
        df = pd.DataFrame({ "PAT_MRN_ID" : [9641956,9641956,8227510,8165474,7860000],
                            "Problem" : ["Headache","Stomach-Ache","Headache","Chicken-Pox","Stomach-Ache"]
                         })
        
            PAT_MRN_ID  Problem
        0   9641956     Headache
        1   9641956     Stomach-Ache
        2   8227510     Headache
        3   8165474     Chicken-Pox
        4   7860000     Stomach-Ache
        
        
        df=pd.get_dummies(df, columns=['Problem'],prefix='',prefix_sep='')
             .groupby(['PAT_MRN_ID'], as_index=False)
             .max()
        
        
            PAT_MRN_ID  Chicken-Pox Headache    Stomach-Ache
        0   7860000     0           0           1
        1   8165474     1           0           0
        2   8227510     0           1           0
        3   9641956     0           1           1
        
        

        【讨论】:

          猜你喜欢
          • 2020-05-30
          • 1970-01-01
          • 2011-09-30
          • 1970-01-01
          • 2015-02-04
          • 1970-01-01
          • 2021-01-22
          • 2019-10-31
          • 1970-01-01
          相关资源
          最近更新 更多