pandas 通过第三个函数Map Dataframe 按单元合并两个 Dataframe

g6baxovj  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(171)

考虑df_adf_b,其中

  1. df_a.index == df_b.index
  2. df_a.columns == df_b.columns
  3. df_a.dtypes == df_b.dtypes == float
    现在我有了另一个 Dataframe op,其中
  4. op.index == df_a.index
  5. op.shape[0] == 1,即op是向量(pd.Series),并且每行(元素)具有operator(a: float, b: float) -> bool的形式
    我想实现df_a <op> df_b,即ab的每个单元格必须使用operator进行比较,结果df_op应该与df_adf_b具有相同的形状。
b4qexyjb

b4qexyjb1#

如果opSeries,则可以创建 Dataframe 列表并通过广播传递给numpy.select

np.random.seed(2023)
df1 = pd.DataFrame(np.random.rand(10,4)).add_prefix('col')
df2 = pd.DataFrame(np.random.rand(10,4)).add_prefix('col')

op = pd.Series(np.random.choice(['+','-','*','/'], size=10), index=df1.index)
print (df1)
       col0      col1      col2      col3
0  0.321988  0.890422  0.588052  0.126596
1  0.141341  0.467896  0.022090  0.727275
2  0.524387  0.544935  0.456373  0.501382
3  0.394469  0.151172  0.360875  0.162077
4  0.337959  0.180323  0.390991  0.035648
5  0.564862  0.203461  0.320604  0.376564
6  0.184054  0.103952  0.454927  0.195864
7  0.378525  0.930532  0.760160  0.770764
8  0.596701  0.791621  0.810338  0.980557
9  0.884785  0.109801  0.819711  0.307613

print (df2)
       col0      col1      col2      col3
0  0.261495  0.405724  0.553420  0.625526
1  0.078760  0.972283  0.411311  0.721664
2  0.663287  0.218225  0.187173  0.729779
3  0.863313  0.391720  0.110048  0.912792
4  0.357006  0.412962  0.183550  0.585990
5  0.855671  0.789681  0.087842  0.932993
6  0.499951  0.364257  0.483606  0.515225
7  0.994643  0.753403  0.365820  0.611373
8  0.419611  0.091677  0.534854  0.340764
9  0.018341  0.603453  0.921806  0.281364
print (op)
0    -
1    +
2    -
3    +
4    /
5    *
6    *
7    +
8    +
9    *
dtype: object
v = [df1.add(df2), df1.sub(df2), df1.mul(df2), df1.div(df2)]

arr = op.to_numpy()[:, None]
m = [(arr == '+'),(arr == '-'),(arr == '*'),(arr == '/')]

df = pd.DataFrame(np.select(m, v), index=df1.index, columns=df1.columns)
print (df)
       col0      col1      col2      col3
0  0.060494  0.484699  0.034632 -0.498930
1  0.220101  1.440179  0.433401  1.448939
2 -0.138900  0.326710  0.269201 -0.228397
3  1.257782  0.542893  0.470923  1.074869
4  0.946647  0.436658  2.130166  0.060834
5  0.483336  0.160670  0.028163  0.351331
6  0.092018  0.037865  0.220006  0.100914
7  1.373169  1.683935  1.125980  1.382137
8  1.016312  0.883298  1.345192  1.321321
9  0.016228  0.066260  0.755614  0.086551

如果op等于DataFrame
一个三个三个一个
编辑:如果需要由操作员进行比较:

np.random.seed(2023)
df1 = pd.DataFrame(np.random.rand(10,4)).add_prefix('col')
df2 = pd.DataFrame(np.random.rand(10,4)).add_prefix('col')

op = pd.Series(np.random.choice(['>','!=','=='],size=10))

print (df1)
       col0      col1      col2      col3
0  0.321988  0.890422  0.588052  0.126596
1  0.141341  0.467896  0.022090  0.727275
2  0.524387  0.544935  0.456373  0.501382
3  0.394469  0.151172  0.360875  0.162077
4  0.337959  0.180323  0.390991  0.035648
5  0.564862  0.203461  0.320604  0.376564
6  0.184054  0.103952  0.454927  0.195864
7  0.378525  0.930532  0.760160  0.770764
8  0.596701  0.791621  0.810338  0.980557
9  0.884785  0.109801  0.819711  0.307613

print (df2)
       col0      col1      col2      col3
0  0.261495  0.405724  0.553420  0.625526
1  0.078760  0.972283  0.411311  0.721664
2  0.663287  0.218225  0.187173  0.729779
3  0.863313  0.391720  0.110048  0.912792
4  0.357006  0.412962  0.183550  0.585990
5  0.855671  0.789681  0.087842  0.932993
6  0.499951  0.364257  0.483606  0.515225
7  0.994643  0.753403  0.365820  0.611373
8  0.419611  0.091677  0.534854  0.340764
9  0.018341  0.603453  0.921806  0.281364
print (op)
0    !=
1     >
2    !=
3     >
4    ==
5    ==
6     >
7     >
8    ==
9    ==
dtype: object
arr = op.to_numpy()[:, None]

df = ((df1.eq(df2) & (arr == '==')) | 
      (df1.gt(df2) & (arr == '>')) |
      (df1.ne(df2) & (arr == '!=')))
      
print (df)
    col0   col1   col2   col3
0   True   True   True   True
1   True  False  False   True
2   True   True   True   True
3  False  False   True  False
4  False  False  False  False
5  False  False  False  False
6  False  False  False  False
7  False   True   True   True
8  False  False  False  False
9  False  False  False  False

相关问题