【问题标题】:How to compare all columns with one column in pandas?如何将所有列与熊猫中的一列进行比较?
【发布时间】:2017-08-06 01:34:16
【问题描述】:

对于下面的df

                A       B       ..... THRESHOLD             
DATE                                       
2011-01-01       NaN       NaN  .....      NaN   
2012-01-01 -0.041158 -0.161571  ..... 0.329038   
2013-01-01  0.238156  0.525878  ..... 0.110370   
2014-01-01  0.606738  0.854177  ..... -0.095147   
2015-01-01  0.200166  0.385453  ..... 0.166235 

我必须将 N 列如 A、B、C .... 与 THRESHOLD 进行比较,并输出结果如

df['A_CALC'] = np.where(df['A'] > df['THRESHOLD'], 1, -1)
df['B_CALC'] = np.where(df['B'] > df['THRESHOLD'], 1, -1)

如何将上述内容应用于所有列(A、B、C ...)而不显式地为每列编写一个语句?

【问题讨论】:

    标签: python pandas numpy dataframe


    【解决方案1】:

    你可以使用df.apply:

    In [670]: df.iloc[:, :-1]\
                .apply(lambda x: np.where(x > df.THRESHOLD, 1, -1), axis=0)\
                .add_suffix('_CALC')
    Out[670]: 
                A_CALC  B_CALC
    Date                      
    2011-01-01      -1      -1
    2012-01-01      -1      -1
    2013-01-01       1       1
    2014-01-01       1       1
    2015-01-01       1       1
    

    如果THRESHOLD 不是你的最后一列,你最好使用

    df[df.columns.difference(['THRESHOLD'])].apply(lambda x: np.where(x > df.THRESHOLD, 1, -1), axis=0).add_suffix('_CALC')
    

    【讨论】:

    • 我认为你可以只在pandas 中使用where 函数,而不是从numpy,仍然是一个不错的解决方案
    【解决方案2】:

    或者你可以试试这个,使用subtract,应该比apply

    (df.drop(['THRESHOLD'],axis=1).subtract(df.THRESHOLD,axis=0)>0)\
        .astype(int).replace({0:-1}).add_suffix('_CALC')
    

    【讨论】:

      【解决方案3】:

      以下就足够了吗?

      for col in df.columns.values:
          if col!= 'THRESHOLD':
              newname = col+'_CALC'
              df[newname] = np.where(df[col] > df['THRESHOLD'], 1, -1)
      

      【讨论】:

      • 从不建议在处理 pandas 列时使用 for 循环。
      • 哎哟!这是为什么?我从来没有遇到过问题,尽管我可以想象它非常耗时......
      • 正是因为它很耗时:]
      • 当你到期的规模很大时,你会发现那些for循环杀死了运行时间,使可能成为不可能
      【解决方案4】:

      我需要将一些列与一列进行比较(更改某些列并保持某些列不变)。我在上面使用了cs95的答案并设置了一个索引。

      • 要保留在索引中的列(假设 col1 和 col2)。
      • 如果任何不在索引中的列大于 col2,则它得到 1,否则为 0。

      数据:

      df=pd.DataFrame({'col1':range(10,15), 'col2':range(1,6), 'col3':np.random.randn(5)+3,'col4':np.random.randn(5)+3,'col5':np.random.randn(5)})
      
          col1    col2    col3        col4        col5
      0   10      1       2.741873    2.402274    -1.208714
      1   11      2       3.328949    2.692367    -0.813730
      2   12      3       5.074692    3.155199    -0.721969
      3   13      4       2.725135    3.393867    -2.452344
      4   14      5       3.626220    3.002514    -0.897204
      

      代码:

      import numpy as np
      
      df['col2_copy'] = df['col2']
      df=df.set_index(['col1','col2'])
      df=df.apply(lambda x: np.where(x > df['col2_copy'], 1, 0), axis=0).reset_index().drop(['col2_copy'],axis = 1)
      

      输出:

          col1    col2    col3    col4    col5
      0   10      1       1       1       0
      1   11      2       1       1       0
      2   12      3       1       1       0
      3   13      4       0       0       0
      4   14      5       0       0       0
      

      【讨论】:

        猜你喜欢
        • 2017-01-31
        • 2019-09-11
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-08
        相关资源
        最近更新 更多