问题描述
我喜欢将特定文件夹中的两个csv文件读取到两个单独的数据帧中.
I like to read two csv files from a particular folder into two separate dataframes.
两个文件名为:23314621_MACI_NAV.CSV和23314623_MACI_Holding.CSV
The two file names are: 23314621_MACI_NAV.CSV and 23314623_MACI_Holding.CSV
文件名的文件第二部分是固定的MACI_NAV.CSV和MACI_Holding.CSV,但是文件名的第一部分(即数字)每天都会更改.
The file second part of the file names are fixed MACI_NAV.CSV and MACI_Holding.CSV, however the first part of the file name which are numbers change everyday.
我喜欢通过尝试将它们读入两个不同的数据帧中:
I like to read them into two different dataframe by trying this:
import pandas as pd
import glob
msci_folder = 'N:/Operation/Daily CDS E_Report/CDS/MACI/'
mscifile = glob.glob(msci_folder + "\*.csv")
for file in mscifile:
df_nav=pd.read_csv(file)
df_holding=pd.read_csv(file)
似乎两行都在读取同一文件,如何使它们读取不同的文件(第二个文件)?
It seems like both lines are reading the same file, how do I make them read different files (second file)?
推荐答案
如果要创建DataFrame列表:
If want create list of DataFrames:
dfs = []
for file in mscifile:
df = pd.read_csv(file)
dfs.append(df)
或使用列表理解:
dfs = [pd.read_csv(file) for file in mscifile]
print (dfs[0])
print (dfs[1])
另一种解决方案是使用文件名中_
之后的最后一个子字符串用键创建dictionary of DataFrames
:
Another solution is create dictionary of DataFrames
with keys by last substring after _
in filename:
from os.path import splitext, basename
dfs = {splitext(basename(fp))[0].split('_')[-1] : pd.read_csv(fp) for fp in mscifile}
print (dfs)
print (dfs['NAV'])
print (dfs['Holding'])
这篇关于使用 pandas 将多个csv文件读取到单独的数据帧中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!