使用Reduce Combine_first合并Pandas DataFrame时,出现错误AttributeError:'str'对象没有属性'combine_first'

时间:2019-07-02 20:28:15

标签: python pandas

使用中提供的示例,

Merging Panda DataFrame on Index, with adding additional column, and not having duplicate index

试图合并DataFrame列表

将名称重置为“ ts”的功能

def setTSIndex(df):
            colNames=df.columns.values
            colNames[0]="ts"
            df.columns=colNames
            df['ts']=df['ts'].astype('str')
            df=df.set_index('ts')
            df
  

上述方法用于调整“名称”列

frames=np.array([]) # To append a list of Data Frames
# frames accumulates, a list of combined pairs of data frame.

# df1 & df2 a pair of  are passed from a loop, 
# df1 & df2 are combined , and then appended to frames

#for df1,df2 in Some list of df1,df2.... 
    setTSIndex(df1)
    setTSIndex(df2)
    combined=pd.concat([dfCpu, dfMem], axis=1, sort=False)
    frames=np.append(frames,combined)

# Below code is outside the loop
from functools import reduce
df=reduce(lambda x,y: x.combine_first(y), frames)

错误

<ipython-input-67-e5cc05efa853> in <lambda>(x, y)
     37             #print(cpuSheet,memSheet)
     38 from functools import reduce
---> 39 df=reduce(lambda x,y: x.combine_first(y), frames)
     40 df.head()

AttributeError: 'str' object has no attribute 'combine_first'

0 个答案:

没有答案