【问题标题】:Add the missing value from one dataframe column to another column using python pandas使用 python pandas 将一个数据框列中的缺失值添加到另一列
【发布时间】:2022-06-11 13:42:48
【问题描述】:

我有两个不同的 Excel 文件,我使用 pd.readExcel 读取它们。第一个 excel 文件是一种主文件,它有很多列。仅显示相关的列: df1

Company Name                                              Excel Company ID
0                                    cleverbridge AG      IQ109133656
1  BT España, Compañía de Servicios Globales de T...        IQ3806173
2                                   Technoserv Group       IQ40333012
3                                    Blue Media S.A.       IQ50008102
4            zeb.rolfes.schierenbeck.associates gmbh       IQ30413992

第二个 excel 基本上是一个输出 excel 文件,如下所示: df2

company_id          found_keywords  no_of_url                                       company_name
0  IQ137156215      insurance         15                         Zühlke Technology Group AG
1    IQ3806173      insurance         15  BT España, Compañía de Servicios Globales de T...
2   IQ40333012      insurance          4                                   Technoserv Group
3   IQ51614192      insurance         15                             Octo Telematics S.p.A.

我希望此输出 excel 文件/df2 包含来自 df1 的那些 company_id 和公司名称,其中来自 df1 的公司 ID 和公司名称不是 df2 的一部分。像这样的东西: df2

company_id found_keywords  no_of_url                                       company_name
0  IQ137156215      insurance         15                         Zühlke Technology Group AG
1    IQ3806173      insurance         15  BT España, Compañía de Servicios Globales de T...
2   IQ40333012      insurance          4                                   Technoserv Group
3   IQ51614192      insurance         15                             Octo Telematics S.p.A.
4   IQ30413992      NaN               NaN              zeb.rolfes.schierenbeck.associates gmbh          

我尝试了几种通过使用pd.mergenp.where 来实现此目的的方法,但都没有成功。我需要做什么才能按预期工作。

【问题讨论】:

    标签: python excel pandas dataframe


    【解决方案1】:

    你是如何使用合并的?您可以将 df1 列和 merge 重命名为 how='outer'

    df1.rename({'Company Name': 'company_name', 'Excel Company ID': 'company_id'}, axis=1, inplace=True)
    print(df2.merge(df1, how='outer'))
    

    输出:

        company_id found_keywords  no_of_url                                       company_name
    0  IQ137156215      insurance       15.0                         Zühlke Technology Group AG
    1    IQ3806173      insurance       15.0  BT España, Compañía de Servicios Globales de T...
    2   IQ40333012      insurance        4.0                                   Technoserv Group
    3   IQ51614192      insurance       15.0                             Octo Telematics S.p.A.
    4  IQ109133656            NaN        NaN                                    cleverbridge AG
    5   IQ50008102            NaN        NaN                                    Blue Media S.A.
    6   IQ30413992            NaN        NaN            zeb.rolfes.schierenbeck.associates gmbh
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-22
      • 2016-03-28
      • 2021-03-30
      • 1970-01-01
      • 2019-01-28
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多