【问题标题】:How to compare 2 dataframes columns and add a value to a new dataframe based on the result如何比较 2 个数据框列并根据结果向新数据框添加值
【发布时间】:2019-08-29 12:42:04
【问题描述】:

我有 2 个长度相同的数据框,我想比较它们之间的特定列。如果一个数据框中第一列的值更大 - 我希望它采用第二列中的值并将其分配给新的数据框。 见例子。第一个数据框:

       0   class
0    1.9       0
1    9.8       0
2    4.5       0
3    8.1       0
4    1.9       0

第二个数据框:

       0   class
0    1.4       1
1    7.8       1
2    8.5       1
3    9.1       1
4    3.9       1

新的数据框应该如下所示:

  class
0     0
1     0
2     1
3     1
4     1

【问题讨论】:

    标签: pandas compare


    【解决方案1】:

    numpy.whereDataFrame 构造函数一起使用:

    df = pd.DataFrame({'class': np.where(df1[0] > df2[0], df1['class'], df2['class'])})
    

    DataFrame.where:

    df = df1[['class']].where(df1[0] > df2[0], df2[['class']])
    

    print (df)
       class
    0      0
    1      0
    2      1
    3      1
    4      1
    

    编辑:

    如果有其他情况,请使用numpy.select,如有必要,请使用numpy.isclose

    print (df2)
         0  class
    0  1.4      1
    1  7.8      1
    2  8.5      1
    3  9.1      1
    4  1.9      1
    
    
    masks = [df1[0] == df2[0], df1[0] > df2[0]]
    #if need compare floats in some accuracy
    #masks = [np.isclose(df1[0], df2[0]), df1[0] > df2[0]]
    vals = ['not_determined', df1['class']]
    df = pd.DataFrame({'class': np.select(masks, vals, df2['class'])})
    print (df)
                class
    0               0
    1               0
    2               1
    3               1
    4  not_determined
    

    或者:

    masks = [df1[0] == df2[0], df1[0] > df2[0]]
    vals = ['not_determined', 1]
    df = pd.DataFrame({'class': np.select(masks, vals, 1)})
    print (df)
                class
    0               0
    1               0
    2               1
    3               1
    4  not_determined
    

    开箱即用解决方案:

    df = np.sign(df1[0].sub(df2[0])).map({1:0, -1:1, 0:'not_determined'}).to_frame('class')
    print (df)
                class
    0               0
    1               0
    2               1
    3               1
    4  not_determined
    

    【讨论】:

    • 谢谢!那很完美。如何处理值相同的情况?假设在这些情况下我希望它写“not_determined”
    • @Bella - 嗯,给我一些时间。
    • 漂亮的解决方案!
    【解决方案2】:

    既然class是0和1,你可以试试,

    df1[0].lt(df2[0]).astype(int)
    

    对于通用解决方案,请查看 jezrael 的答案。

    【讨论】:

      【解决方案3】:

      试试这个:

      >>> import numpy as np
      >>> import pandas as pd
      >>> df_1
           0  class
      0  1.9      0
      1  9.8      0
      2  4.5      0
      3  8.1      0
      4  1.9      0
      >>> df_2
           0  class
      0  1.4      1
      1  7.8      1
      2  8.5      1
      3  9.1      1
      4  3.9      1
      >>> df_3=pd.DataFrame()
      >>> df_3["class"]=np.where(df_1["0"]>df_2["0"], df_1["class"], df_2["class"])
      >>> df_3
         class
      0      0
      1      0
      2      1
      3      1
      4      1
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-29
        • 1970-01-01
        • 2019-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        相关资源
        最近更新 更多