Home > Net >  Loading multiple json files with respective file name into dataframe
Loading multiple json files with respective file name into dataframe

Time:09-21

Reading the json files into the dataframe currently works this way but the file name is the same one for the two different files. Where have I gone wrong?

json_files = glob.glob(r"json_files\*.json")
df = pd.DataFrame()
for _,ele in enumerate(json_files,len(json_files)):
    df = pd.concat([df, pd.read_json(ele)])
    df['filename'] = os.path.basename(ele).strip(".json")
    df = df.drop(['pages'], axis=1)

CodePudding user response:

Or, How about this code?

json_files = glob.glob(r"json_files\*.json")
dfs = []
for _,ele in enumerate(json_files,len(json_files)):
    #df = pd.concat([df, pd.read_json(ele)])
    df = pd.read_json(ele)
    df['filename'] = os.path.basename(ele).strip(".json")
    df = df.drop(['pages'], axis=1)
    dfs.append(df)
df_final = pd.concat(dfs)

  • Related