python
Pandas - Delete,Remove,Drop, column from pandas DataFrame
# Opiton one
del df['column_name']
# The best way to do this in pandas is to use drop:
df = df.drop('column_name', 1)
# where 1 is the axis number (0 for rows and 1 for columns.) To delete the column without having to reassign df you can do:
df.drop('column_name', axis=1, inplace=True)
# Finally, to drop by column number instead of by column label, try this to delete, e.g. the 1st, 2nd and 4th columns:
df = df.drop(df.columns[[0, 1, 3]], axis=1) # df.columns is zero-based pd.Index this is called drop by index
# Another way is:
columns = ['Col1', 'Col2', ...]
df.drop(columns, inplace=True, axis=1)
Was this helpful?
Similar Posts
- Delete the first column in a Pandas DataFrame
- [Pandas] Add new column to DataFrame based on existing column
- Change column orders using column names list - Pandas Dataframe
- Delete one or multiple columns from Pandas Dataframe
- Pandas - Delete multiple rows from DataFrame using index list
- Reorder dataframe columns using column names in pandas
- Get a value from DataFrame row using index and column name in pandas