1 回答
TA贡献1809条经验 获得超8个赞
如果输入数据在json文件中使用:
cols = ['Date','x','y','z']
df = pd.DataFrame(pd.read_json('json.json', lines=True)['data'].tolist(), columns=cols)
df['Date'] = pd.to_datetime(df['Date'], unit='s')
print (df)
Date x y z
0 2020-10-21 06:18:43.328814030 0.171875 -0.960938 0.023438
1 2020-10-21 06:18:45.060513735 0.085938 -0.984375 0.000000
2 2020-10-21 06:18:46.353275299 0.964979 NaN NaN
3 2020-10-21 06:18:47.698888779 0.039062 -1.000000 0.125000
4 2020-10-21 06:18:48.853050232 0.078125 -0.992188 0.000000
如果输入DataFrame带有列col:
cols = ['Date','x','y','z']
df = pd.DataFrame(pd.json_normalize(df['col'])['data'].tolist(), columns=cols)
df['Date'] = pd.to_datetime(df['Date'], unit='s')
print (df)
Date x y z
0 2020-10-21 06:18:43.328814030 0.171875 -0.960938 0.023438
1 2020-10-21 06:18:45.060513735 0.085938 -0.984375 0.000000
2 2020-10-21 06:18:46.353275299 0.964979 NaN NaN
3 2020-10-21 06:18:47.698888779 0.039062 -1.000000 0.125000
4 2020-10-21 06:18:48.853050232 0.078125 -0.992188 0.000000
编辑:
就个人而言,像这样保存 csv.xls并不是一个好主意,因为这样会read_excel引发奇怪的错误,但您可以使用:
import ast
df = pd.read_csv('15-10-2020-OO.xls')
cols = ['Date','x','y','z']
data = [x['data'] for x in df['Data'].apply(ast.literal_eval)]
df = pd.DataFrame(data, columns=cols)
df['Date'] = pd.to_datetime(df['Date'], unit='s')
print (df)
Date x y z
0 2020-10-15 07:21:16.159236193 0.085938 -0.972656 0.003906
1 2020-10-15 07:21:17.597931385 0.089844 -0.968750 0.003906
2 2020-10-15 07:21:18.838171959 0.089844 -0.972656 0.003906
3 2020-10-15 07:21:20.338105917 0.085938 -0.972656 0.003906
4 2020-10-15 07:21:21.768864155 0.089844 -0.984375 0.003906
... ... ... ...
8457 2020-10-15 08:59:57.907007933 0.085938 -0.972656 0.003906
8458 2020-10-15 08:59:58.371274233 0.089844 -0.976562 0.003906
8459 2020-10-15 08:59:58.833237648 0.085938 -0.976562 0.003906
8460 2020-10-15 08:59:59.313337088 1.517057 NaN NaN
8461 2020-10-15 08:59:59.863240004 0.089844 -0.968750 0.007812
[8462 rows x 4 columns]
添加回答
举报