简体   繁体   English

自动将多个json文件(具有不同的信息)读取并保存到不同的pandas数据帧

[英]Automate the read and save of several json files (with different information) to different pandas dataframes

I have several json files in a folder, each one with different shapes (number of lines and columns) and information. 我在一个文件夹中有几个json文件,每个文件都有不同的形状(行数和列数)和信息。

I have the following code to open and save a json file to a pandas df: 我有以下代码打开并将json文件保存到pandas df:

with open('f_fruit.json', 'r') as f:
    data = json.load(f)

df_fruit = pd.DataFrame(data['fruit'])

In the end, I would like to have different pandas dataframes, one for each json file: 最后,我想拥有不同的pandas数据框,每个json文件一个:

df_fruit

df_clothes

df_games

What is the best way to automate this code, considering that the files names and information do not follow a pattern? 考虑到文件名和信息不遵循模式,自动执行此代码的最佳方法是什么? Is it possible? 可能吗?

Assuming that your files are named following the same logic I would do the following: 假设您的文件使用相同的逻辑命名,我将执行以下操作:

files = ['f_fruit.json','f_clothes.json','f_games.json'] #you can use os.walk to get a list of files from a specific folder

for file_name in files:
    col_name = file_name.split('.')[0][2:]
    with open(file_name, 'r') as f:
        data = json.load(f)
    var_name = 'df_{}'.format(col_name)
    globals()[var_name] = pd.DataFrame(data[col_name])

However, if 但是,如果

files names and information do not follow a pattern 文件名和信息不遵循模式

then there is no easy way to automate this. 那么就没有自动化的自动化方法。 You need a pattern. 您需要一个模式。

Here the part you are probably interested in, ie how to create a variable from a value already in memory using globals() . 在这里,您可能感兴趣的部分,即如何使用globals()从内存中已有的值创建变量。

>>> col_name = 'fruit'
>>> var_name = 'df_{}'.format(col_name)
>>> globals()[var_name] = 'some value'
>>> df_fruit
'some value'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM