将数据框字典合并为1个单个数据框 [英] Combine dictionary of dataframes into 1 single dataframe
本文介绍了将数据框字典合并为1个单个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种解决方案,将字典中的所有数据帧都放入一个单一的巨型数据帧中.我是Python的新手,所以我无法理解如何遍历字典并将所有数据帧放入1.到目前为止,我已实现的代码如下:
I am looking for a solution to put all my dataframes which are in a dictionary into 1 single giant dataframe. I am relatively new to Python so I am unable to understand how to iterate over a dictionary and put all the dataframes into 1. The code I have implemented so far, is as below:
import sys
from ftplib import FTP
import os
import socket
import time
import pandas as pd
import numpy as np
from glob import glob
path = 'path_to_file'
files = glob(path + '/*Mail*.xlsx')
print files
get_df = lambda f: pd.read_excel(f, sheetname=None)
dodf = {f: get_df(f) for f in files} ### dictionary of dataframes
现在,我需要将所有不同的数据帧放入1个数据帧中,然后对它进行操作.任何建议将不胜感激.
Now, I need to put all the different dataframes into 1 dataframe and then do my operations on that. Any advise would be appreciated.
我已经尝试过了
for df in dodf:
pd.concat(dodf.values(), ignore_index=True)
但这似乎行不通.
推荐答案
我认为需要 concat
,具有dict理解:
I think need concat
with dict comprehension:
dodf = {f: pd.read_excel(f, sheet_name=None) for f in files}
df = pd.concat([pd.concat(v) for k,v in dodf.items()])
这篇关于将数据框字典合并为1个单个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文