6

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

3 Answers 3

2

you can use this

df['a'][df['a']==0]
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You can use iloc to extract a row as a series, then apply your condition:

row = df.iloc[0]           # extract first row as series
res = row[res == 1].index  # filter for values equal to 1 and get columns via index

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.