9951 explained code solutions for 126 technologies


python-pandasHow to add column to data frame


import pandas as pd

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

data['Color'] = ['black', 'black', 'black', 'red', 'red']ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

['Color']

adds Color column to dataframe


Usage example

import pandas as pd

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

data['Color'] = ['black', 'black', 'black', 'red', 'red']

print(data)
output
  Phone  Phone Price  Color
0   ip5          204  black
1   ip6          304  black
2   ip8          404  black
3   sms          405    red
4    xi          305    red