bml
bml

Reputation: 125

How to filter dataframe and make a subset at once pandas

I am trying to make a subselection of a dataframe based on some columns, while at the same time filtering the dataframe based on a different column. In SQL it looks like this:

SELECT col1, col2, col3,
FROM table
WHERE colume_4 = some_value

I know how to do it in two steps, but I prefer doing it in one operation. Does anyone know how to do this in python?

Upvotes: 1

Views: 683

Answers (2)

Ruthger Righart
Ruthger Righart

Reputation: 4921

An alternative way:

df[df['col4']==3].filter(['col1', 'col2', 'col3'])

Upvotes: 0

jezrael
jezrael

Reputation: 862731

Use DataFrame.loc with combination with boolean indexing:

df.loc[df.colume_4 == some_value, ['col1','col2','col3']]

Upvotes: 1

Related Questions