how to delete a column in a df in python

To delete a column in a pandas DataFrame in Python, you can use the drop method with the axis parameter set to 1. Here's an example:

main.py
import pandas as pd

# create a sample dataframe
data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'gender': ['F', 'M', 'M']}
df = pd.DataFrame(data)

# drop the 'gender' column
df = df.drop('gender', axis=1)

# print the resulting dataframe
print(df)
268 chars
12 lines

Output:

main.py
       name  age
0     Alice   25
1       Bob   30
2  Charlie   35
67 chars
5 lines

In the code above, we first create a sample DataFrame df with three columns (name, age, and gender). We then drop the gender column using the drop method with axis=1. Finally, we print the resulting DataFrame to verify that the column was indeed deleted.

gistlibby LogSnag