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}
{'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':'
I read this data into a pandas DataFrame, looking something like this:
我将这些数据读入pandas DataFrame,看起来像这样:
address date1 date2 date3 date4 date5 date6 date7 <email> NaN NaN NaN 1 NaN NaN NaN
地址date1 date2 date3 date4 date5 date6 date7
I then use the following methods to calculate the mean and standard deviation and add them to the DataFrame:
然后,我使用以下方法计算平均值和标准差,并将它们添加到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)
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?
当我打印df时,它看起来应该是这样。但是,当我使用此方法将DataFrame写入JSON字符串df.to_json(
1 个解决方案
#1
1
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.
然后导出到JSON。
Edit: ok, I see that you see it works with print df now sorry).
编辑:好的,我看到你看到它适用于打印df现在抱歉)。
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:
这是带有输出的json:
http://www.jsoneditoronline.org/?id=c0b29191d89fba8b593e29009af4f382
#1
1
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.
然后导出到JSON。
Edit: ok, I see that you see it works with print df now sorry).
编辑:好的,我看到你看到它适用于打印df现在抱歉)。
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:
这是带有输出的json:
http://www.jsoneditoronline.org/?id=c0b29191d89fba8b593e29009af4f382