简体   繁体   中英

select series from transposed pandas dataframe

With a dataframe, called mrna as follows:

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)?

I tried: mrna.iloc['ERBB2'] but that only takes a integer, and doesn't map to string

I also tried:

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

but I get 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

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".

mrna.set_index('id').T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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