Lostsoul
Lostsoul

Reputation: 25999

pandas using apply method and sending column names?

I have a pandas dataframe that I want to convert each row to a json message to upload. I figured this would be a great usecase for the apply method but I'm having a slight problem, it's not sending the column names.

Here's my data:

    Industry    CountryName periodDate  predicted
0   Advertising Agencies    USA 1995.0  144565.060000
1   Advertising Agencies    USA 1996.0  165903.120000
2   Advertising Agencies    USA 2001.0  326320.740300

When I use apply I lose the column names(industry, countryName, periodDate, etc)

def sendAggData(row):
  uploadDataJson = row.to_json(orient='records')
  print(json.loads(uploadDataJson))

aggValue.apply(sendAggData, axis=1)

I get this result:

['Advertising Agencies', 'USA', 1995.0, 144565.06]
['Advertising Agencies', 'USA', 1996.0, 165903.12]
['Advertising Agencies', 'USA', 2001.0, 326320.7403]

I want this as a json message, so I'd like the column name on it. so something like {'Industry': 'Advertising Agencies', 'CountryName':'USA'....} - previously I got this to work using a for loop for each row but was told that apply is the more pandas way :-) Any suggestion of what I can do to use apply correctly?

Upvotes: 2

Views: 41

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

You can just do:

df.apply(lambda x: x.to_json(), axis=1)

Which gives you:

0    {"Industry":0,"CountryName":"Advertising Agenc...
1    {"Industry":1,"CountryName":"Advertising Agenc...
2    {"Industry":2,"CountryName":"Advertising Agenc...
dtype: object

However, what's about df.to_dict('records') which gives:

[{'Industry': 0, 'CountryName': 'Advertising Agencies', 'periodDate': 'USA 1995.0', 'predicted': 144565.06},
 {'Industry': 1, 'CountryName': 'Advertising Agencies', 'periodDate': 'USA 1996.0', 'predicted': 165903.12}, 
 {'Industry': 2, 'CountryName': 'Advertising Agencies', 'periodDate': 'USA 2001.0', 'predicted': 326320.7403}]

Upvotes: 1

Related Questions