如何用for循环命名数据框? [英] How to name dataframes with a for loop?

查看:121
本文介绍了如何用for循环命名数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取几个文件json文件,并使用for循环将它们写入数据框.

I want to read several files json files and write them to a dataframe with a for-loop.

review_categories = ["beauty", "pet"]

for i in review_categories:
    filename = "D:\\Library\\reviews_{}.json".format(i)
    output = pd.read_json(path_or_buf=filename, lines=True)
return output 

问题是我希望每个评论类别都有自己的变量,例如一个名为"beauty_reviews"的数据框,另一个名为"pet_reviews"的数据框,其中分别包含从reviews_beauty.json和reviews_pet.json读取的数据.

The problem is I want each review category to have its own variable, like a dataframe called "beauty_reviews", and another called "pet_reviews", containing the data read from reviews_beauty.json and reviews_pet.json respectively.

推荐答案

我认为处理字典中的数据帧很容易.尝试以下代码:

I think it is easy to handle the dataframes in a dictionary. Try the codes below:

review_categories = ["beauty", "pet"]
reviews = {}

for review in review_categories:
     df_name = review + '_reviews' # the name for the dataframe
     filename = "D:\\Library\\reviews_{}.json".format(review)

     reviews[df_name] = pd.read_json(path_or_buf=filename, lines=True)

评论中,您将具有一个带有各自数据框的键,用于存储数据.如果要检索数据,只需调用:

In reviews, you will have a key with the respective dataframe to store the data. If you want to retrieve the data, just call:

reviews["beauty_reviews"]

希望有帮助.

这篇关于如何用for循环命名数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆