How to name dataframes with a for loop?

☆樱花仙子☆ 提交于 2020-02-23 07:16:21

问题


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 

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.


回答1:


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"]

Hope it helps.




回答2:


You can first pack the files into a list

reviews = []
review_categories = ["beauty", "pet"]
for i in review_categories:
    filename = "D:\\Library\\reviews_{}.json".format(i)
    reviews.append(pd.read_json(path_or_buf=filename, lines=True))

and then unpack your results into the variable names you wanted:

beauty_reviews, pet_reviews = reviews


来源:https://stackoverflow.com/questions/42977487/how-to-name-dataframes-with-a-for-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!