TypeError:合并时没有初始值的空序列的reduce()

时间:2016-10-05 16:25:26

标签: python pandas

我的数据框看起来像这样:

DF1

Value    Hectares_2006
1        10
5        15

DF2

Value   Hectares_2007
1       20
5       5

DF3

Value  Hectares_2008
1      22
5      3

我希望将它们全部合并在一起,首先将所有数据帧放在一个列表中,然后使用:

dfs = reduce(lambda left, right: pd.merge(left, right, on=['Value'], how='outer'), list1

但这会返回:

 File "E:/python codes/temp.py", line 32, in <module>
    dfs=reduce(lambda left, right: pd.merge(left, right, on=['VALUE'], how='outer'), list1)

TypeError: reduce() of empty sequence with no initial value

我想要的输出是:

Value  Hectares_2006   Hectares_2007   Hectares_2008
1      10              20              22
5      15              5               3

我的完整代码是这样的,files路径指向成为数据帧的所有文件的路径:

import pandas as pd, os
from simpldbf import Dbf5

list1=[]
files=r'E:\Documents\2015 Summer RA\CDL_in_buffer'
for f in os.listdir(files):
    if '.dbf' in f and '.xml' not in f:
        table=Dbf5(os.path.join(files,f))
        df=table.to_dataframe()
        columns=['VALUE', 'CLASS_NAME','Count']
        df=df[columns]
        if ('2006' in f) or ('2007' in f) or ('2008' in f) or ('2009' in f):
            df['Hectares']=df.Count*0.3136
        if ('2010' in f) or ('2011' in f) or ('2012' in f) or ('2013' in f) or ('2014' in f) or ('2015' in f):
            df['Hectares']=df.Count*0.09
        df.drop(['Count'], axis=1, inplace=True)
        df=df[df['CLASS_NAME'] .isin (['Corn'])]
        df.rename(columns={'CLASS_NAME': 'Crop_' + f.split('.')[0], 'Hectares': 'Hectares_' + f.split('.')[0] }, inplace=True)
        list1.append(df)
    dfs=reduce(lambda left, right: pd.merge(left, right, on=['VALUE'], how='outer'), list1)

1 个答案:

答案 0 :(得分:1)

如评论中所述,您需要取消缩进.dbf行,使其超出for循环。否则,如果看到的第一个文件不包含delegate R Pure<A, R>(A a); delegate R Impure<A, R>(A a); ,则Action a = whatever; ExecuteMethod e = a.Invoke; 在循环的第一次迭代时将为空,这将导致空序列错误。