How to filter columns in Pandas?

In this post you can find how to select columns in Pandas

# select first 5 columns
df.iloc[:, :5]

# select last 5 columns
df.iloc[:, :5]

# select columns with head and tail
df.T.head().T

# select columns by name
df[["col_1", "col_2"]]

# select columns by name
df.loc[:, 'col_1':'col_3']

# select mulitple columns
df.loc[:, ['col_1' , 'col_2', 'col_3']]

# select by column name and index
df[df.columns[2:4]]

# pandas select columns by condition
# there is value greater than 5
df.loc[:, (df > 5).any()]

# all values are greater than 5
df.loc[:, (df > 5).all()] 

In this post we got answer of questions like:

  • get specific columns in Pandas
  • select columns in Pandas
  • pandas select columns by index
  • pandas select columns by name
  • select multiple columns pandas
  • pandas subset dataframe by column value
  • pandas select columns by condition

Resources

Indexing and selecting data