bismo
bismo

Reputation: 1439

How to subset columns in the same line as a .loc in pandas

Efficiency question here. Sometimes I want to run a .loc and look at certain columns after doing so. The way I've always done it is the following

p = df[(df.x==1)]
p(['x','y'])

This would return cells with a value of 1 from column x then subset the dataframe to look at just columns x and y. Is there a way I can do this in one line without the variable assignment? Things I have tried include

df[(df.x==1)].columns(['x','y'])
df[(df.x==1),['x','y'])

I do this a lot and would love if I could make it one line of code. Thanks!

Upvotes: 1

Views: 540

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150745

Don’t chain your index. Instead l, use loc

df.loc[df.x==1, ['x','y']]

The general syntax is

df.loc[index_slice, column_slice]

Upvotes: 2

Jacob
Jacob

Reputation: 568

You can filter for spesific columns of a dataframe by slicing it with a list of column names, like df[['col1', 'col']]. So in your example, you can do that to the filtered dataframe:

df[df.x==1][['x', 'y']]

Upvotes: 1

Related Questions