将其中一列包含字符串形式的数字数组的数据帧转换为json文件



我想将数据帧转换为json文件。数据框架的其中一列以字符串形式包含时间序列。因此,最终的json看起来像这样:

[{"...":"...","Dauer":"24h","Wertereihe":"8619.0,9130.0,8302.0,8140.0"}, {...}, {...}]

是否有可能将df保存到json文件中,以这样的方式,"是一个数字数组吗?这将得到:[{"...":"...","Dauer":"24h","Wertereihe":[8619.0,9130.0,8302.0,8140.0]}, {...}, {...}]

我使用以下代码片段将df保存到json文件:df.to_json(jsonFile, orient = "records")

iuc,您需要:

df['Wertereihe'] = df['Wertereihe'].apply(lambda x: list(map(float, x.split(','))))
df.to_json(jsonFile, orient = "records")

最新更新