pandas creating a new column based on row values
zljubisic at gmail.com
zljubisic at gmail.com
Tue Mar 28 15:36:29 EDT 2017
This doesn't work:
import pandas as pd
def myfunc():
return 'Start_{}_{}_{}_{}_End'.format(df['coverage'], df['name'], df['reports'], df['year'])
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'year': [2012, 2012, 2013, 2014, 2014],
'reports': [4, 24, 31, 2, 3],
'coverage': [25, 94, 57, 62, 70]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
print(df)
df['New'] = df.apply(myfunc(), axis=1)
TypeError: ("'str' object is not callable", 'occurred at index Cochice')
Can I somehow generate a new column by concatenating values for the other columns in a row?
Expected result for column 'New' would be:
Start_Jason_2012_4_25_End
Start_Molly_2012_24_94_End
Start_Tina_2013_31_57_End
Start_Jake_2014_2_62_End
More information about the Python-list
mailing list