简体   繁体   English

从转置熊猫数据框中选择序列

[英]select series from transposed pandas dataframe

With a dataframe, called mrna as follows: 具有一个称为mrna的数据框,如下所示:

id      Cell_1  Cell_2  Cell_3
CDH3    8.006   5.183   10.144
ERBB2   9.799   12.355  8.571
...

How can I select the ERBB2 row as a pandas series (if I don't know its index)? 如何选择ERBB2行作为熊猫系列(如果我不知道它的索引)?

I tried: mrna.iloc['ERBB2'] but that only takes a integer, and doesn't map to string 我试过了: mrna.iloc['ERBB2']但这只需要一个整数,并且不映射到字符串

I also tried: 我也尝试过:

mrna_t = mrna.transpose()
mrna_t['ERBB2'] 

but I get KeyError: 'ERBB2' 但我得到KeyError:'ERBB2'

Pass a boolean condition to generate a boolean mask, this mask is used against the index and will return just the rows where the condition is met: 传递布尔条件以生成布尔掩码,此掩码将用于索引,并将仅返回满足条件的行:

In [116]:
df[df['id']=='ERBB2']

Out[116]:
      id  Cell_1  Cell_2  Cell_3
1  ERBB2   9.799  12.355   8.571

Output from boolean condition: 布尔条件的输出:

In [117]:
df['id']=='ERBB2'

Out[117]:
0    False
1     True
Name: id, dtype: bool

As for your error: mrna_t['ERBB2'] will attempt to look for a column with that name which doesn't exist hence the KeyError 至于您的错误: mrna_t['ERBB2']将尝试查找名称不存在的列,因此KeyError

If it was your index then you could just do: 如果这是您的索引,则可以执行以下操作:

df.loc['ERBB2']

To select index values matching the passed label, it's worth checking the docs , including the section on index selection by position and label 要选择与传递的标签匹配的索引值,值得检查文档 ,包括有关按位置和标签选择索引的部分

Just figured it out. 只是想通了。 I just reset the index labels then transposed. 我只是重置索引标签然后转置。 This allowed me to index by "ERBB2". 这使我可以通过“ ERBB2”编制索引。

mrna.set_index('id').T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM