如何在大Pandas中进行纵列操作?

2024-04-25 16:39:09 发布

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

我有一个数据帧,看起来像:

 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]'

当我清楚地知道它在索引中,所以我想我遗漏了一些概念。非常感谢您的帮助!你知道吗

我应该补充一点,参数值是浮动的,以防万一这也是一个问题!你知道吗


Tags: the数据sampleindataframedfforloc
1条回答
网友
1楼 · 发布于 2024-04-25 16:39:09

方法.loc首先需要行索引,然后是列索引,因此以下操作应该可以执行,因为您希望执行逐列操作:

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

您可以在documentation中找到更多信息。你知道吗

相关问题 更多 >