简体   繁体   中英

Transposing data in pandas, then output to CSV

My knowledge isn't good enough to do this :( I'm starting with data that looks like this:

{"data": {"1547700225.29": -5.3369832056203785, "1547700227.54": -6.044502239243294, "1547700229.7899997": -4.642320938763093, "1547700231.9799995": -5.104047573562501, "1547700234.1799998": -5.104047573562501, "1547700236.4699998": -5.3369832056203785, "1547700238.7699995": -5.807173592599741}}

This is my script so far:

import csv
import json
import pandas as pd
from pandas.io.json import json_normalize
def json_csv():
with open('metis.json') as data_file:
    data=json.load(data_file)
normalized_df = pd.io.json.json_normalize(data)
normalized_df = pd.DataFrame.transpose(normalized_df)
normalized_df.to_csv('metis.csv',index=False)
return
def main():        
    json_csv() 
main()

I need to end up with a CSV file that looks like this:

Time, Value
1547700225.29,-5.3369832056203785
1547700227.54,-6.044502239243294
1547700229.7899997,-4.642320938763093
1547700231.9799995,-5.104047573562501
1547700234.1799998,-5.104047573562501
1547700236.4699998,-5.3369832056203785
1547700238.7699995,-5.807173592599741

When I run the commands interactively, the transpose works so it's something with the data structure &/or the to_csv method. I just don't know enough to make it work.

Also the Time is epoch time and I need to convert to a human readable datetime.

Any tips please ? I don't mind researching if pointed in the right direction but I've been reading a lot and I can't hit on the right terminology in order to find the answer.

Use:

df = pd.read_json('metis.json').reset_index()
df.columns = ['Time', 'Value']
df.to_csv('output.csv')

# Also has human readable time
+---+-------------------------+-----------+
|   |          Time           |   Value   |
+---+-------------------------+-----------+
| 0 | 2019-01-17 04:43:45.290 | -5.336983 |
| 1 | 2019-01-17 04:43:47.540 | -6.044502 |
| 2 | 2019-01-17 04:43:49.790 | -4.642321 |
| 3 | 2019-01-17 04:43:51.980 | -5.104048 |
| 4 | 2019-01-17 04:43:54.180 | -5.104048 |
| 5 | 2019-01-17 04:43:56.470 | -5.336983 |
| 6 | 2019-01-17 04:43:58.770 | -5.807174 |
+---+-------------------------+-----------+

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM