9951 explained code solutions for 126 technologies


python-pandasHow to remove multiple columns from dataframe


import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 404, 405, 305],
  'Color': ['red', 'red', 'gray', 'black', 'red']
})

df.drop(['Price', 'Color'], inplace=True, axis=1)ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

.drop(

drops selected column from dataframe

['Price', 'Color']

list of columns to remove

inplace=True

will remove column and overwrite current dataframe variable

axis=1

used to ask Pandas to drop column (not row)


Usage example

import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 404, 405, 305],
  'Color': ['red', 'red', 'gray', 'black', 'red']
})

df.drop(['Price', 'Color'], inplace=True, axis=1)
print(df)
output
  Phone
0   ip5
1   ip6
2   ip8
3   sms
4    xi