【问题标题】:How to merge three dataframes based on row indices?如何根据行索引合并三个数据框?
【发布时间】:2021-09-10 23:10:50
【问题描述】:

我正在尝试根据它们的行索引合并三个数据框。但是,on 属性不会将 index 作为选项。有没有更好的方法来合并数据帧,而不必将行索引作为列写入每个数据帧?

from functools import reduce

dfs = [result_eu_SpeciesNameGenuine, result_ieu_SpeciesNameGenuine, result_cosine_SpeciesNameGenuine]

df_final = reduce(lambda left,right: pd.merge(left,right,on=index), dfs)

df_final

【问题讨论】:

  • df.merge(df2, left_index=True, right_index=True)
  • 使用pd.Dataframe.join,它接受一个列表作为其他:dfs[0].join(dfs[1:])

标签: python dataframe indexing merge row


【解决方案1】:

根据文档使用pd.DataFrame.join 尝试此操作,其他可以是数据框列表:

dfs[0].join(dfs[1:])

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多