Home > OS >  Convert a data frame in which one column contains array of numbers as string to a json file
Convert a data frame in which one column contains array of numbers as string to a json file

Time:10-21

I'd like to convert a data frame into a json file. One of the columns of the data frame contains time series as a string. Thus, the final json looks like this:

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

Is it possible to save the df to a json file in such a way that "Wertereihe" is an array of numbers? This would give: [{"...":"...","Dauer":"24h","Wertereihe":[8619.0,9130.0,8302.0,8140.0]}, {...}, {...}]

I used the following snippet to save the df to a json file: df.to_json(jsonFile, orient = "records")

CodePudding user response:

IIUC, you need:

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