9951 explained code solutions for 126 technologies


python-pandasHow to select specific 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']
})

filtered = df[['Phone', 'Color']]ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

['Phone', 'Color']

list of columns to select fromdf dataframe

filtered

resulting dataframe with only selected columns