如何在pandas中区分两个dataframe,除了一个column?

2024-10-03 19:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个具有相同列名的数据帧。在

         wave   num   stlines      fwhm        EWs  MeasredWave
 0    4050.32   3.0  0.282690  0.073650  22.160800  4050.311360
 1    4208.98   5.5  0.490580  0.084925  44.323130  4208.973512
 2    4374.94   9.0  0.714830  0.114290  86.964970  4374.927110
 3    4379.74   9.0  0.314040  0.091070  30.442710  4379.760601
 4    4398.01  14.0  0.504150  0.098450  52.832360  4398.007473
 5    4502.21   8.0  0.562780  0.101090  60.559960  4502.205220


         wave  num   stlines      fwhm        EWs  MeasredWave
 0    4050.32  3.0  0.276350  0.077770  22.876240  4050.310469
 1    4208.98  5.5  0.493035  0.084065  44.095755  4208.974363
 2    4374.94  6.0  0.716760  0.111550  85.111070  4374.927649
 3    4379.74  1.0  0.299070  0.098400  31.325300  4379.759339
 4    4398.01  6.0  0.508810  0.084530  45.783740  4398.004164
 5    4502.21  9.0  0.572320  0.100540  61.252070  4502.205764

因为这两个数据帧都有列名,而第1列在两个数据帧中都是相同的。我想取除列1以外的所有列的差,即wave。在

因此,结果数据帧应该有column1和所有其他列的差异

我怎么能做到呢?在


Tags: 数据差异wavenumcolumn1ewsfwhmstlines
1条回答
网友
1楼 · 发布于 2024-10-03 19:33:18

我认为需要按^{}提取列名称,然后使用^{}

cols = df1.columns.difference(['wave'])
#is possible specify multiple columns
#cols = df1.columns.difference(['wave','MeasredWave'])

#df1[cols] = means in output are not touch columns from df1
df1[cols] = df1[cols].sub(df2[cols])
print (df1)
      wave  num   stlines     fwhm       EWs  MeasredWave
0  4050.32  0.0  0.006340 -0.00412 -0.715440     0.000891
1  4208.98  0.0 -0.002455  0.00086  0.227375    -0.000851
2  4374.94  3.0 -0.001930  0.00274  1.853900    -0.000539
3  4379.74  8.0  0.014970 -0.00733 -0.882590     0.001262
4  4398.01  8.0 -0.004660  0.01392  7.048620     0.003309
5  4502.21 -1.0 -0.009540  0.00055 -0.692110    -0.000544

^{pr2}$

相关问题 更多 >