【问题标题】:Compare Array on multiple condition on column level in python?在python中的列级别上比较多个条件的数组?
【发布时间】:2020-05-05 20:45:11
【问题描述】:
df1:
  stu_Id  sub1  sub2  sub3
0  1001    45    68    69
1  1002    67    78    57
2  1003    76    68    78
3  1004    87    56    69
4  1005    45    43    73

df2 :
  stu_Id  sub1  sub2  sub3
0  1001    45    68    69
1  1002    45    68    69
2  1003    45    68    69
3  1004    45    68    69
4  1005    45    68    69


cond1= df1.to_numpy[:,[1]]>df2.to_numpy[:,[1]] then 1 else 0

cond2= df1.to_numpy[:,[2]]<df2.to_numpy[:,[2]] then 1 else 0

cond3= df1.to_numpy[:,[3]]-df2.to_numpy[:,[3]]>5 then 1 else 0

如何在 df1 中一次在列级别(sub1,sub2,sub3)上应用上述三个数组条件。 我试过了

df1['sub1','sub2','sub3']=
np.select[[cond1,cond2,cond3],. 
[1,2,3],0]

这给了我错误。

输出:

stu_Id    sub1  sub2  sub3
0  1001    0     0     0
1  1002    1     0     0
2  1003    1     0     1
3  1004    1     1     0
4  1005    0     1     0

【问题讨论】:

    标签: python python-3.x pandas python-2.7 dataframe


    【解决方案1】:

    您可以使用np.where

    In [1925]: import numpy as np
    
    In [1926]: sub1 = np.where(df1.iloc[:,1] > df2.iloc[:,1], 1, 0)                                                                                                                                             
    
    In [1927]: sub2 = np.where(df1.iloc[:,2] < df2.iloc[:,2], 1, 0)                                                                                                                                             
    
    In [1928]: sub3 = np.where((df1.iloc[:,3] - df2.iloc[:,3]) > 5, 1, 0)  
    
    In [1932]: pd.DataFrame({'stud_Id':df2.stu_Id.tolist(), 'sub1': sub1, 'sub2': sub2, 'sub3': sub3})                                                                                                          
    Out[1932]: 
       stud_Id  sub1  sub2  sub3
    0     1001     0     0     0
    1     1002     1     0     0
    2     1003     1     0     1
    3     1004     1     1     0
    4     1005     0     1     0
    

    【讨论】:

      【解决方案2】:

      将 'stu_Id' 设置为索引并从 df1 中减去 df2

      m = df1.set_index('stu_Id').sub(df2.set_index('stu_Id'))
      m
      
      
             sub1 sub2    sub3
      stu_Id          
      1001    0      0    0
      1002    22    10    -12
      1003    31    0     9
      1004    42  -12     0
      1005    0   -25     4
      

      根据条件创建函数并map它们到相应的列

      def sub1(x):
          if x > 0 :
              return 1
          else:
              return 0
      
      def sub2(x):
          if x < 0 : 
              return 1
          else:
              return 0
      
      def sub3(x):
          if x > 5:
              return 1
          else :
              return 0
      
      m.sub1 = m.sub1.map(sub1)
      m.sub2 = m.sub2.map(sub2)
      m.sub3 = m.sub3.map(sub3)
      
      m
      
            sub1  sub2    sub3
      stu_Id          
      1001    0    0       0
      1002    1    0       0
      1003    1    0       1
      1004    1    1       0
      1005    0    1       0
      

      您也可以使用numpy where 来复制相同的函数,每列,应该更快。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 2022-12-18
        • 1970-01-01
        相关资源
        最近更新 更多