Pandas Dataframe-在特定行中选择具有特定值的列 [英] Pandas Dataframe - select columns with a specific value in a specific row

查看:207
本文介绍了Pandas Dataframe-在特定行中选择具有特定值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Pandas Dataframe的特定行(例如第一行)中选择具有特定值(例如1)的列

I want to select columns with a specific value (say 1) in a specific row (say first row) for Pandas Dataframe

推荐答案

使用 iloc boolean indexing ,因为性能更好地过滤了index而不是DataFrame,然后选择索引(请参见性能):

Use iloc with boolean indexing, for performance is better filtering index not DataFrame and then select index (see performance):

df = pd.DataFrame({
        'A':list('abcdef'),
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
         'F':list('aaabbb')
})

print (df)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  b  5  8  3  3  a
2  c  4  9  5  6  a
3  d  5  4  7  9  b
4  e  5  2  1  2  b
5  f  4  3  0  4  b

s = df.iloc[0]        
a = s.index[s == 1]
print (a)
Index(['D'], dtype='object')

a = s.index.values[(s == 1)]
print (a)
['D']

这篇关于Pandas Dataframe-在特定行中选择具有特定值的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆