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.py27 chars2 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.py19 chars2 lines
This will assign the values from the B
column to the variable b_column
.
gistlibby LogSnag