[英]Reading multiple files in the same folder to different dataframes
I have a few csv files in same folder an I need to add them to different dataframes of the same name.我在同一个文件夹中有几个 csv 文件,我需要将它们添加到同名的不同数据帧中。 I am using the below code
我正在使用下面的代码
for file in files:
file_name = file.split('.')
if file_name[1] == 'csv':
file_name[0] = pd.read_csv(file)
else:
continue
The list of files is ['', 'bond_ratings', 'fund_allocations', 'fund_config', 'fund_ratios', 'fund_specs', 'Hack', 'other_specs', 'return_10year', 'return_3year', 'return_5year']文件列表是 ['', 'bond_ratings', 'fund_allocations', 'fund_config', 'fund_ratios', 'fund_specs', 'Hack', 'other_specs', 'return_10year', 'return_3year', 'return_5year']
However, when I try say 'bond_ratings.head()' it says bind_ratings is not defined.但是,当我尝试说“bond_ratings.head()”时,它说没有定义 bind_ratings。 Where am I wrong.
我哪里错了。
You can create dict of DataFrames:您可以创建 DataFrames 的字典:
d = {}
for file in files:
file_name = file.split('.')
if file_name[1] == 'csv':
d[file_name[0]] = pd.read_csv(file)
And then select by keys:然后通过按键 select :
print (d['bond_ratings'])
What you need is not recommended , but possible:不推荐您需要的,但可能:
for file in files:
file_name = file.split('.')
if file_name[1] == 'csv':
globals()[file_name[0]] = pd.read_csv(file)
print (bond_ratings)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.