To subset a dataframe by column names I usually do:
df[['A', 'B']]
where list(df.columns.values) = ['A', 'B', 'C', 'D']
Say I instead wanted to get all the columns except 'B'. How would I do this? This apparently does not work:
df[!['B']]
Use the drop method:
df.drop('B', axis=1)
drop can also accept a list of columns, if you wish to drop more than one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With