Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据文件名,将csv文件放在单独的数据框中_Python_Pandas_Csv_Dataframe_Concat - Fatal编程技术网

Python 根据文件名,将csv文件放在单独的数据框中

Python 根据文件名,将csv文件放在单独的数据框中,python,pandas,csv,dataframe,concat,Python,Pandas,Csv,Dataframe,Concat,我有一个包含文件名的列表。我想解析目录,读取从列表中的每个元素开始的所有文件,并将其存储在dataframe中 例如: 目录: abc1.txt abc2.txt abc3.txt bcd1.txt bcd2.txt bcd3.txt 输出应确保以“abc”开头的文件位于一个数据帧中,以“bcd”开头的文件位于其他数据帧中,等等 我的代码: dfs = [] for exp in expnames: for files in filenames: if

我有一个包含文件名的列表。我想解析目录,读取从列表中的每个元素开始的所有文件,并将其存储在dataframe中

例如:

目录:

abc1.txt   
abc2.txt
abc3.txt

bcd1.txt
bcd2.txt
bcd3.txt
输出应确保以“abc”开头的文件位于一个数据帧中,以“bcd”开头的文件位于其他数据帧中,等等

我的代码:

 dfs = []
 for exp in expnames:
     for files in filenames:
         if files.startswith(exp):
              dfs.append(pd.read_csv(file_path+files,sep=',',header=None))
      big_frame = pd.concat(dfs, ignore_index=True)

这将创建一个
DataFrames
字典,其中每个
DataFrames
包含与“表达式”(即
abc
def
et.c.)前三个字母匹配的所有文件。字典中的键是相同的三个字母:

# Some dummy data
filenames = ['abcdefghijkl.txt', 'abcdef.txt',  'defghijk.txt']

# List of combination of certain letters 
exps = ['abc', 'def', 'ghi', 'jkl']

dataframes = {} 
for filename in filenames:
    _df = pd.read_csv(filename)

    key = exps[exps.index(filename[:3])]

    try: 
        dataframes[key] = pd.concat([dataframes[key], _df], ignore_index=True)
    except KeyError:
        dataframes[key] = _df



print(dataframes['abc'])

    a   b   c
0   7   8   9
1  10  11  12
2   1   2   3
3   4   5   6

print(dataframes['def'])
    a   b   c
0   7   8   9
1  10  11  12
上述文件的内容包括:

abcdefghijkl.txt

abcdef.txt

defghijkl.txt


我假设你有一个目录,除了你想读的文件之外,还有其他几个文件

import os
import pandas as pd

dfs = { }

for f in os.listdir(dirname):
   for k in list1:
       if f.startswith(k):
          try:
             dfs[k].concat(pd.read_csv(dirname+f, sep=',', header=None))
          except KeyError:
             dfs[k] = pd.read_csv(dirname+f, sep=',', header=None))

没有
DataFrame.concat()
方法,因此此操作将中断。您需要使用
DataFrames
列表调用
pd.concat()
,或者使用
DataFrames.append()
。不建议这样做<循环中的code>concat是低效的,是错误的。文档中指出,显式for循环中的
concat
效率低于列表理解,但并未说明出于性能以外的其他原因,不建议在循环中使用
concat
。这通常适用于
python
。正如你所知,性能不是一切。否则,您可以开始对每个答案进行向下投票,以便使用显式for循环而不是列表理解。祝你好运,@jpp。
a,b,c
7,8,9
10,11,12
a,b,c
1,2,3
4,5,6
a,b,c
7,8,9
10,11,12
import os
import pandas as pd

dfs = { }

for f in os.listdir(dirname):
   for k in list1:
       if f.startswith(k):
          try:
             dfs[k].concat(pd.read_csv(dirname+f, sep=',', header=None))
          except KeyError:
             dfs[k] = pd.read_csv(dirname+f, sep=',', header=None))