1

I realize that this may seem like a question that has already been asked here, but none of the solutions seem to work. I start out with a dictionary that looks something along the line of this:

{'2016-05-08': 1, '2016-05-09': nan, '2016-05-05': nan, '2016-05-06': nan, '2016-05-07': nan, '2016-05-11': nan, 'address': '<email address>, '2016-05-12': nan, '2016-05-10': nan}

I read this data into a pandas DataFrame, looking something like this:

address date1 date2 date3 date4 date5 date6 date7 <email> NaN NaN NaN 1 NaN NaN NaN

I then use the following methods to calculate the mean and standard deviation and add them to the DataFrame:

mean = pd.Series(df.mean(axis=1), index=df.index) std = pd.Series(df.std(axis=1), index=df.index) df = pd.concat([df, mean, std], axis=1)

When I print df, it looks as it should. However, when I used this method to write the DataFrame to a JSON string, df.to_json(<path to file>), it get the original dictionary in my JSON file. I want a JSON string of all the data with the standard deviation and mean included in the JSON data, how can I do this?

1 Answer 1

2

If your summary columns give you the data you expect, add the columns to the dataframe.

Try

df['std'] = pd.Series(df.std(axis=1), index=df.index)

df['mean'] = pd.Series(df.mean(axis=1), index=df.index)

Then export to JSON.

Edit: ok, I see that you see it works with print df now sorry).

I was unable to reproduce your results. this is what I have:

import pandas as pd

d = {'2016-05-08': 1, '2016-05-09': float('nan'), '2016-05-05': float('nan'), '2016-05-06': float('nan'), '2016-05-07': float('nan'), '2016-05-11': float('nan'), 'address': '<email address>', '2016-05-12': float('nan'), '2016-05-10': float('nan')}

df = pd.DataFrame(d, index=[0])

mean = pd.Series(df.mean(axis=1), index=df.index)
std = pd.Series(df.std(axis=1), index=df.index)
df = pd.concat([df, mean, std], axis=1)

df.to_json('correctoutput.txt')

Here is the json with the output:

http://www.jsoneditoronline.org/?id=c0b29191d89fba8b593e29009af4f382

Sign up to request clarification or add additional context in comments.

5 Comments

Isn't that what the concat method does?
Doesn't change output
@GrantSteiner, aside from the above edit, try this df.to_json() without the file to see if you get the right output.
Figured it out. It was an issue with other code in the program. Thank you for your help.
@grant steiner glad I could help. Marking this as the answer would be appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.