drop two columns
时间: 2023-08-05 16:01:59 浏览: 44
To drop two or more columns from a dataframe in Python using Pandas library, you can pass a list of column names to the drop() method. Here is an example code snippet:
```
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9], 'D': [10, 11, 12]})
# drop two columns by specifying the column names in a list and axis=1
df = df.drop(['C', 'D'], axis=1)
# print the resulting dataframe
print(df)
```
This will output the following dataframe with the 'C' and 'D' columns dropped:
```
A B
0 1 4
1 2 5
2 3 6
```
Here, we pass a list of the column names ('C' and 'D') to the drop() method, separated by commas. We also specify axis=1 to indicate that we want to drop columns (as opposed to rows, which would be axis=0). Finally, we assign the resulting dataframe back to the variable df to update it with the dropped columns.