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

2024-10-03 17:17:39 发布

您现在位置:Python中文网/ 问答频道 /正文

使用中给出的示例

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

试图合并数据帧列表

函数将列名称重置为“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

The Above Method is used to adjust the column Name

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'

Tags: oflambdafromreducedfframeslistfirst