将目录中的所有csv文件作为pandas dfs导入,并将其命名为csv文件名 [英] import all csv files in directory as pandas dfs and name them as csv filenames

查看:92
本文介绍了将目录中的所有csv文件作为pandas dfs导入,并将其命名为csv文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本会将目录中的所有.csv文件作为数据帧导入到我的工作区中.每个数据框都应命名为csv文件(减去扩展名:.csv).

I'm trying to write a script that will import all .csv files in a directory to my workspace as dataframes. Each dataframe should be named as the csv file (minus the extension: .csv).

这是我到目前为止的内容,但是努力理解如何在循环中为数据帧分配正确的名称.我见过建议使用exec()的帖子,但这似乎不是一个很好的解决方案.

This is what i have so far, but struggling to understand how to assign the correct name to the dataframe in the loop. I've seen posts that suggest using exec() but this does not seem like a great solution.

path = "../3_Data/Benefits"                     # dir path
all_files = glob.glob(os.path.join(path, "*.csv")) #make list of paths

for file in all_files:
    dfn = file.split('\\')[-1].split('.')[0] # create string for df name
    dfn = pd.read_csv(file,skiprows=5) # This line should assign to the value stored in dfn

任何帮助表示感谢,谢谢.

Any help appreciated, thanks.

推荐答案

DataFrame没有name,它们的索引可以有name.这是设置方法.

DataFrame have no name their index can have a name. This is how to set it.

import glob
import os

path = "./data/"
all_files = glob.glob(os.path.join(path, "*.csv")) #make list of paths

for file in all_files:
    # Getting the file name without extension
    file_name = os.path.splitext(os.path.basename(file))[0]
    # Reading the file content to create a DataFrame
    dfn = pd.read_csv(file)
    # Setting the file name (without extension) as the index name
    dfn.index.name = file_name

# Example showing the Name in the print output

#      FirstYear  LastYear
# Name                     
# 0         1990      2007
# 1         2001      2001
# 2         2001      2008

这篇关于将目录中的所有csv文件作为pandas dfs导入,并将其命名为csv文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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