9951 explained code solutions for 126 technologies


python-pandasHow to keep only certain columns in dataframe


In order to keep only specific columns, we can select needed columns and overwrite our 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 = df[['Phone', 'Color']]ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

['Phone', 'Color']

list of columns to keep in df dataframe


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 = df[['Phone', 'Color']]

print(df)
output
  Phone  Color
0   ip5    red
1   ip6    red
2   ip8   gray
3   sms  black
4    xi    red