Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 测试dataframe中列之间的关系_Python_Pandas - Fatal编程技术网

Python 测试dataframe中列之间的关系

Python 测试dataframe中列之间的关系,python,pandas,Python,Pandas,我有这个数据框: a b c d e 0 1.35 0.35 0.20 0.15 1.0 1 3.22 1.50 0.50 1.0 1.72 ... 请注意,a始终等于b+d,b始终等于c+d。 我希望能够以某种方式指出这种关系,在某种程度上,它看起来是这样的: a b c d e 0 1.35 0.35 0.20 0.15 1.0 1 3.22 1

我有这个数据框:

     a      b     c     d    e
0 1.35   0.35  0.20  0.15  1.0
1 3.22   1.50  0.50  1.0  1.72  
...
请注意,
a
始终等于
b+d
b
始终等于
c+d
。 我希望能够以某种方式指出这种关系,在某种程度上,它看起来是这样的:

      a      b        c     d    e
0     1.35   0.35  0.20  0.15  1.0
1     3.22   1.50  0.50  1.0  1.72  
level    1      2     3    3     2

或者其他方式。 有人能帮忙吗

编辑
我不想知道如何添加具有这些值的行,我想检测列之间的关系。

您可以始终在列名中添加级别

df = pd.DataFrame({"a":[1.35,3.22],"b":[0.35,1.5],"c":[0.2,0.5],"d":[0.15,1],"e":[1,1.72]})

cols = [df.columns,
       ["b+e","c+d","-","-","-"]]
df.columns=pd.MultiIndex.from_tuples(list(zip(*cols)))
print(df.to_string(index=False))
输出

    a     b    c     d     e
  b+e   c+d    -     -     -
 1.35  0.35  0.2  0.15  1.00
 3.22  1.50  0.5  1.00  1.72

那么您想在数据帧中添加一行吗?或者您想让数据框自动计算列之间的关系?我想让数据框自动计算列之间的关系实际上我想让代码检测这些关系,而不仅仅是添加一个rowwell感谢您对我的答案投了否决票。。。。最好总是花时间定义需求,这样解决方案就更容易找到了
    a     b    c     d     e
  b+e   c+d    -     -     -
 1.35  0.35  0.2  0.15  1.00
 3.22  1.50  0.5  1.00  1.72