Say you have a dataframe:
import pandas as pd
sales = [{'account': 'Jones LLC', 'nuts': 150, 'bolts': 200, 'totalval': 140, 'Cur': 'pesos'},
{'account': 'Alpha Co', 'nuts': 200, 'bolts': 210, 'totalval': 215, 'Cur': 'euros'},
{'account': 'Blue Inc', 'nuts': 50, 'bolts': 90, 'totalval': 95 , 'Cur': 'pounds'}]
mydf = pd.DataFrame(sales)
And you would like to generate a string celebrating the month's profits. For instance:
"We made 140 pesos from Jones LLC. Wahoo!"
My first attempt to solve this would be to take a template string with placeholders and formatting it with the monthly numbers row by row. Note that these monthly numbers are integers, not strings.
celebstring = "We made amt Cur from Jones LLC. Woohoo!"
def createpr(inputdf):
for index, row in inputdf.iterrows():
filledstring = celebstring.replace("amt","{0}".format(str(row["totalval"]))).replace('Cur','{0}'.format(str(row['Cur'])))
inputdf['fullstring'] = filledstring
return inputdf
df2 = createpr(mydf)
But when you run this code the 'fullstring' field for ALL the rows is only populated with the values from the last row. The dataframe looks like this (note I have removed two columns for readability):
sales = [{'account': 'Jones LLC','totalval': 140, 'Cur': 'pesos', 'fullstring': 'We got an order totaling 95 pounds selling parts wahoo!'},
{'account': 'Alpha Co','totalval': 215, 'Cur': 'euros', 'fullstring': 'We got an order totaling 95 pounds selling parts wahoo!'},
{'account': 'Blue Inc','totalval': 95 , 'Cur': 'pounds','fullstring': 'We got an order totaling 95 pounds selling parts wahoo!'}]
How do you get the function to replace the values according to the corresponding values in each row?
It's easier to use format_map
In [40]: mydf.apply('We made {totalval} pesos from {account}. Woohoo!'.format_map, axis=1)
Out[40]:
0 We made 140 pesos from Jones LLC. Woohoo!
1 We made 215 pesos from Alpha Co. Woohoo!
2 We made 95 pesos from Blue Inc. Woohoo!
dtype: object
Assign back with
In [46]: mydf.assign(fullstring=mydf.apply(
'We made {totalval} pesos from {account}. Woohoo!'.format_map, axis=1))
Out[46]:
Cur account bolts nuts totalval \
0 pesos Jones LLC 200 150 140
1 euros Alpha Co 210 200 215
2 pounds Blue Inc 90 50 95
fullstring
0 We made 140 pesos from Jones LLC. Woohoo!
1 We made 215 pesos from Alpha Co. Woohoo!
2 We made 95 pesos from Blue Inc. Woohoo!
For dict
you could use to_dict
In [48]: mydf.assign(fullstring=mydf.apply(
'We made {totalval} pesos from {account}. Woohoo!'.format_map, axis=1)
).to_dict(orient='r')
Out[48]:
[{'Cur': 'pesos',
'account': 'Jones LLC',
'bolts': 200,
'nuts': 150,
'totalval': 140,
'fullstring': 'We made 140 pesos from Jones LLC. Woohoo!'},
{'Cur': 'euros',
'account': 'Alpha Co',
'bolts': 210,
'nuts': 200,
'totalval': 215,
'fullstring': 'We made 215 pesos from Alpha Co. Woohoo!'},
{'Cur': 'pounds',
'account': 'Blue Inc',
'bolts': 90,
'nuts': 50,
'totalval': 95,
'fullstring': 'We made 95 pesos from Blue Inc. Woohoo!'}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.