有没有更好的方法来合并多个数据帧

问题描述 投票:0回答:1
我已经成功制作了一个数据表,以解决我最后一个没有得到答复的问题。我仍然想知道是否有更好的方法。 例如,如果我将所有列导入在一起以使它们仅是函数中的一个参数,并且需要先将它们放入列表中,该怎么办?或者它们可以保留为 csv 文件中的列用于争论吗?

这是我当前的功能

def CHEGD_funct(countylist, Clist, Hlist, Elist, Glist, Dlist): Cdict = pd.DataFrame(list(zip(countylist, Clist)), columns = ['site', 'C']) Hdict = pd.DataFrame(list(zip(countylist, Hlist)), columns = ['site', 'H']) Edict = pd.DataFrame(list(zip(countylist, Elist)), columns = ['site', 'E']) Gdict = pd.DataFrame(list(zip(countylist, Glist)), columns = ['site', 'G']) Ddict = pd.DataFrame(list(zip(countylist, Dlist)), columns = ['site', 'D']) Cdf = Cdict.groupby('site').sum() Hdf = Hdict.groupby('site').sum() Edf = Edict.groupby('site').sum() Gdf = Gdict.groupby('site').sum() Ddf = Ddict.groupby('site').sum() dataframes = [Cdf, Hdf, Edf, Gdf, Ddf] mergedf = reduce(lambda left,right: pd.merge(left,right,on=['site'], how='outer'), dataframes).fillna('void') return mergedf
这就是输出,这就是我想要的。

type C H E G D site Angus 20 92 25 0 0 Angus / East Perthshire 1 8 3 0 0 Argyll 141 995 89 68 7 Ayrshire 71 336 68 17 9 Banffshire 7 86 19 0 0 Banffshire / Moray 2 10 0 0 0 Banffshire / South Aberdeenshire 1 3 0 0 0 Berwickshire 17 84 14 0 1 Caithness 43 374 202 36 5 Clyde Isles 29 142 38 9 5 Dumfriesshire 34 336 69 21 8 Dunbartonshire 38 85 19 10 2 East Inverness-shire & Nairn 162 879 318 45 10 East Inverness-shire & Nairn / Banffshire 1 8 4 0 0 East Inverness-shire / Moray 22 50 23 4 0 East Lothian 22 123 36 16 4 East Perthshire 31 149 79 7 5 East Ross 28 233 65 18 2 East Ross / East Inverness-shire 0 1 0 0 0 East Ross / East Sutherland 1 3 1 0 0 East Sutherland 34 208 71 8 4 Fifeshire 60 355 43 17 1 Kincardineshire 30 160 27 9 1 Kintyre 15 75 11 3 5 Kirkcudbrightshire 21 114 33 4 5 Lanarkshire 94 293 62 25 7 Lanarkshire / Peebleshire 3 7 0 2 0 Mid Ebudes 9 97 30 1 1 Mid Perthshire 46 378 176 26 8 Mid Perthshire / East Perthshire 3 9 3 1 0 Midlothain / Peebleshire 5 23 1 0 0 Midlothian 52 194 47 12 2 Midlothian / Berwickshire 8 20 3 2 1 Moray 60 311 77 11 2 Moray / East Inverness-shire & Nairn 5 14 11 0 1 North Aberdeenshire 38 211 55 10 1 North Ebudes 109 533 207 94 14 Orkney 50 230 126 23 1 Outer Hebrides 26 265 110 29 0 Peebleshire 66 339 114 25 10 Peebleshire / Selkirkshire 0 3 2 0 0 Renfrewshire 76 312 40 39 4 Roxburghshire 76 327 65 21 7 Selkirkshire 28 168 68 3 3 Shetland 57 426 195 13 7 South Aberdeenshire 159 791 320 64 21 South Aberdeenshire / East Perthshire 2 18 12 3 1 South Aberdeenshire / Kincardineshire 1 0 0 0 0 South Aberdeenshire / North Aberdeenshire 0 1 1 0 0 South Ebudes 36 172 38 4 4 Stirlingshire 33 121 26 5 4 West Inverness (Westerness) 0 0 1 0 0 West Inverness-shire 41 443 217 32 3 West Lothian 15 57 11 1 1 West Lothian / Stirlingshire 0 0 0 0 0 West Perthshire 18 107 23 0 1 West Ross 19 277 84 22 2 West Ross / West Sutherland 2 7 2 0 0 West Sutherland 78 610 196 58 4 Wigtownshire 9 46 17 2 1
    
pandas dataframe data-analysis
1个回答
0
投票
您可以创建一个数据框,然后对每列进行分组

def CHEGD_funct(countylist, Clist, Hlist, Elist, Glist, Dlist): # Create a single DataFrame df = pd.DataFrame( {"site":countrylist, "C":Clist, "H": Hlist, "E": Elist, "G": Glist, "D": Dlist, }) site_sums = df.groupby("site").sum().reset_index() return site_sums
    
© www.soinside.com 2019 - 2024. All rights reserved.