【问题标题】:how to check the two rows based on condition and club into single row in python如何根据条件检查两行并将其合并为python中的单行
【发布时间】:2019-11-27 11:29:31
【问题描述】:

我的数据框看起来像:

Id survey suvery_link Primary_call alternate call
1   form1  link1       7/18/19                   
1   form1  link1                      8/18/19
2   form1  link1       8/18/19                 
2   form1  link1                       9/18/19
3   form1  link1       8/18/19                 
3   form1  link1                       9/18/19
4   form1  link1       7/18/19                 
4   form1  link1                       8/18/19
1   form2  link2       8/18/19                   
1   form2  link2                      9/18/19
2   form2  link2       7/18/19                 
2   form2  link2                       8/18/19
3   form2  link2       7/18/19                 
3   form2  link2                       8/18/19
4   form2  link2       8/18/19                 
4   form2  link2                       9/18/19

我正在尝试获取新的数据框,如下所示

Id survey suvery_link Primary_call alternate call
1   form1  link1       7/18/19       8/18/19
1   form2  link2       8/18/19       9/18/19             
2   form1  link1       8/18/19       9/18/19
2   form2  link2       7/18/19       8/18/19          
3   form1  link1       8/18/19       9/18/19
3   form2  link2       7/18/19       8/18/19
4   form1  link1       7/18/19       8/18/19
4   form2  link2       8/18/19       9/18/19

我使用了以下代码但不是代码

df.sort_values(['Id','survey',survey_link','Primary_call','alternate call']).drop_duplicate('ID')

它不工作

【问题讨论】:

    标签: python pandas loops dataframe if-statement


    【解决方案1】:

    这是一个很好的解决方案:

    table.groupby(['Id','survey','suvery_link'], as_index=False)[['Primary_call','alternate_call']].max()
    

    【讨论】:

      【解决方案2】:
      df.groupby(['Id','survey','suvery_link'], as_index=False)[['Primary_call','alternate_call']].max()
      
      

      【讨论】:

        【解决方案3】:

        我建议将日期处理为日期时间,您可以使用max()

        df['Primary_call'] = pd.to_datetime(df['Primary_call'])
        df['alternate_call'] = pd.to_datetime(df['alternate_call'])
        
        df.groupby('Id', as_index=False).max()
        

        【讨论】:

        • 它不起作用,请再次查看问题
        猜你喜欢
        • 2021-12-31
        • 2018-02-02
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 2020-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多