【发布时间】:2016-12-17 15:41:51
【问题描述】:
我正在尝试使用循环来读取多个 CSV(现在但将来会混合使用它和 xls)。
我希望 pandas 中的每个数据框都具有相同的名称,不包括我文件夹中的文件扩展名。
import os
import pandas as pd
files = filter(os.path.isfile, os.listdir( os.curdir ) )
files # this shows a list of the files that I want to use/have in my directory- they are all CSVs if that matters
# i want to load these into pandas data frames with the corresponding filenames
# not sure if this is the right approach....
# but what is wrong is the variable is named 'weather_today.csv'... i need to drop the .csv or .xlsx or whatever it might be
for each_file in files:
frame = pd.read_csv( each_file)
each_file = frame
Bernie 看起来很棒但是有一个问题:
or each_file in files:
frame = pd.read_csv(each_file)
filename_only = os.path.splitext(each_file)[0]
# Right below I am assigning my looped data frame the literal variable name of "filename_only" rather than the value that filename_only represents
#rather than what happens if I print(filename_only)
filename_only = frame
例如,如果我的两个文件在我的文件列表中分别是 weather_today、地震.csv(按此顺序),则不会创建“地震”和“天气”。
但是,如果我简单地键入“filename_only”并在 python 中单击回车键 - 那么我将看到地震数据框。如果我有 100 个文件,则列表循环中的最后一个数据框名称将标题为“filename_only”,而其他 99 个则不会,因为以前的分配从未进行过,第 100 个会覆盖它们。
【问题讨论】: