简体   繁体   English

如何在python中使用文件名重命名数据框?

[英]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. 我需要读取一个文件夹中的几个csv文件,并对每个文件执行相同的操作。 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)}

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

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