python pandas dataframe superposition json structure flat method

Asked 2 years ago, Updated 2 years ago, 63 views

df_list = [pd.read_json(file, lines=True) for file in glob('./data/*.json')]
df_all = pd.concat(df_list, ignore_index=True)
df_pick = df_all[['data','site']]

>>> df_pick 
                    data                               site
    0    {'key1':'value1', 'key2':'value2'...}    naver

I'd like to flat the column data among the data frame columns declared in the above df_pick variable.

So,

test = pd.json_normalize(df_pick, record_path='data', meta = 'site')

I tried it like this, but it didn't unfold...

However,

pd.json_normalize(df_all['data'])

When I do this, I return the data frame that is flat as I want.

How can I flat while both data and stay columns exist?

python pandas dataframe

2022-09-20 15:56

1 Answers

df_pick = pd.concat(df_all[["site"]], pd.json_normalize(df_all["data"]), axis=1)

Wouldn't it work like this way.


2022-09-20 15:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.