读取多个文件并将其使用pandas压缩为一个DataFrame

时间:2019-02-28 16:07:12

标签: python pandas

我有一张大桌子,可以将我的代码下载到几个csv文件中。然后,我尝试读取每个文件并将它们连接到一个DF,以对数据进行计算。我这样做是这样的:

    def merge_multiple_dfs(self):
        path = tmp_path, f"*.csv"
        all_files = glob.glob(os.path.join(tmp_path, f"*.csv"))
        df_list = []
        logger.info(f'reading the fiiles: {all_files} from {path} to DataFrame')

        for f in all_files:
            df_list.append(pd.read_csv(f))

        df = pd.concat((pd.read_csv(f) for f in df_list))
        shutil.rmtree(self.tmp_path)
        df.to_csv('combined.csv')

我收到以下错误:

Traceback (most recent call last):
  File "/Users/u/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3291, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-659a373a29b2>", line 1, in <module>
    pd.concat((pd.read_csv(f) for f in df_list))
  File "/Users/u/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 228, in concat
    copy=copy, sort=sort)
  File "/Users/u/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 259, in __init__
    objs = list(objs)
  File "<ipython-input-12-659a373a29b2>", line 1, in <genexpr>
    pd.concat((pd.read_csv(f) for f in df_list))
  File "/Users/u/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/pandas/io/parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Users/u/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/pandas/io/parsers.py", line 413, in _read
    filepath_or_buffer, encoding, compression)
  File "/Users/u/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/pandas/io/common.py", line 232, in get_filepath_or_buffer
    raise ValueError(msg.format(_type=type(filepath_or_buffer)))
ValueError: Invalid file path or buffer object type: <class 'pandas.core.frame.DataFrame'>

csv文件具有相同的格式,并且具有相同的列。

有什么想法吗?

0 个答案:

没有答案