简体   繁体   中英

How to rename dataframe with file name in python?

I have several csv files in a folder that I need to read and do the same thing to each file. I want to rename each dataframe that is created with the file name, but am not sure how. Could I store the file names in a list and then refer to them later somehow...? My current code is bellow. Thank you in advance.

import os
Path = "C:\Users\DATA"
filelist = os.listdir(Path)
for x in filelist:
    RawData = pd.read_csv("C:\Users\DATA\%s" % x)

What if you have just one dataframe with all files?

import os
path = "C:\Users\DATA"
raw_data = {i: pd.read_csv(os.path.abspath(i)) for i in os.listdir(path)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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