how to subset a dataframe based on column name in python

To subset a dataframe based on column name in Python, you can use indexing with the column name inside square brackets []. For example, if you have a dataframe called df with columns A, B and C and you want to subset only B and C, you can do the following:

main.py
subset_df = df[['B', 'C']]
27 chars
2 lines

The result will be a new dataframe with only the columns B and C. You can also select a single column by passing a string instead of a list:

main.py
b_column = df['B']
19 chars
2 lines

This will assign the values from the B column to the variable b_column.

related categories

gistlibby LogSnag