not select columns of a dataframe from a list of those columns in python

In order to drop a list of columns from a dataframe in python, you can simply use the .drop() method and pass in the list of columns to drop. Here's an example:

main.py
import pandas as pd

# create a sample dataframe
data = {'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}
df = pd.DataFrame(data)

# create a list of columns to drop
cols_to_drop = ['b', 'c']

# drop the columns from the dataframe
new_df = df.drop(cols_to_drop, axis=1)

print(new_df)
284 chars
14 lines

Output:

main.py
   a
0  1
1  2
2  3
20 chars
5 lines

In the code above, we create a sample dataframe with columns 'a', 'b', and 'c'. We then create a list of columns we want to drop, which is ['b', 'c']. Then we simply use the .drop() method to drop the columns specified in the list. The axis=1 parameter indicates that we want to drop columns, not rows. Finally, we print the new dataframe with only column 'a'.

gistlibby LogSnag