如何在熊猫中进行列式操作?

时间:2018-08-21 18:38:36

标签: python pandas dataframe

我有一个看起来像这样的数据框:

 sample parameter1 parameter2 parameter3
 A      9           6         3        
 B      4           5         7
 C      1           5         8

并且我想执行类似以下操作:

for sample in dataframe:
    df['new parameter'] = df[sample, parameter1]/df[sample, parameter2]

到目前为止,我已经尝试过:

df2.loc['ratio'] = df2.loc['reads mapped']/df2.loc['raw total sequences']

但是我得到了错误:

KeyError: 'the label [reads mapped] is not in the [index]'

当我完全知道它在索引中时,所以我觉得我在某个地方缺少某些概念。任何帮助深表感谢!

我还应该补充一点,参数值是浮点数,以防万一这也是一个问题!

1 个答案:

答案 0 :(得分:0)

方法.loc首先需要行索引,然后是列索引,因此下面的方法应该起作用,因为您想进行按列操作:

 df2['ratio'] = df2.loc[:, 'reads mapped'] / df2.loc[:, 'raw total sequences']

您可以在documentation中找到更多信息。

相关问题