【问题标题】:How to append in a loop for Excel file within pandas如何在 pandas 中的 Excel 文件的循环中追加
【发布时间】:2021-06-29 14:22:19
【问题描述】:

我有两个 DataFrame。 第一个来自 API,第二个来自 Excel 文件。

df1

name description author status
a Bob inactive
b Peter active
c Bob inactive
d Carl active
e Bob inactive
f Peter active

df2

name description
a this is a description
b this is another description

我需要根据 df1 上的每个名称,将 df2 的描述添加到 df1 该怎么做?

【问题讨论】:

    标签: python pandas data-science jupyter


    【解决方案1】:

    您可以使用df.merge 连接两个数据框。

    df1.merge(df2, on='name')
    

    【讨论】:

    • 这会导致冲突列(description_x,description_y)
    • 可以先将描述列放到df1中,然后使用df.merge。那么就不会有冲突了。您也可以删除或重命名该列。
    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2018-10-08
    • 1970-01-01
    • 2018-10-04
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多