Best way to transform JSON data within a Pandas dataframe into a dataframe itself Best way to transform JSON data within a Pandas dataframe into a dataframe itself json json

Best way to transform JSON data within a Pandas dataframe into a dataframe itself


Use ast.literal_eval and apply pd.Series as:

import astdf = df.apply(lambda x: ast.literal_eval(x[0]), 1).apply(pd.Series)print(df)  k1 k2   k3   k40  1  A    X  NaN1  2  B    X  NaN2  3  A    Y  NaN3  4  D  NaN    M

OR:

df = pd.DataFrame([ast.literal_eval(i) for i in df['js']])

OR:

import jsondf = pd.DataFrame([json.loads(i) for i in df['js']])


I would call the dataframe constructor after converting the string to dict ( i think this would be faster):

import astpd.DataFrame(df.js.apply(ast.literal_eval).tolist())

Or:

import jsonpd.DataFrame(df["js"].apply(json.loads).tolist())

  k1 k2   k3   k40  1  A    X  NaN1  2  B    X  NaN2  3  A    Y  NaN3  4  D  NaN    M


You can use apply(pd.Series):

import astprint(df['js'].apply(ast.literal_eval).apply(pd.Series))

Output:

  k1 k2   k3   k40  1  A    X  NaN1  2  B    X  NaN2  3  A    Y  NaN3  4  D  NaN    M