【问题标题】:Fill column of a dataframe from another dataframe从另一个数据框填充数据框的列
【发布时间】:2018-12-08 10:15:11
【问题描述】:

我正在尝试根据条件从另一个数据框中填充数据框的一列。假设我的第一个数据帧是 df1,第二个数据帧名为 df2。

# df1 is described as bellow :
+------+------+
| Col1 | Col2 |
+------+------+
|   A  |  1   |
|   B  |  2   |
|   C  |  3   |
|   A  |  1   |
+------+------+

# df2 is described as bellow :
+------+------+
| Col1 | Col2 |
+------+------+
|   A  |  NaN |
|   B  |  NaN |
|   D  |  NaN |
+------+------+

Col1 的每个不同值都有一个 ID 号(在 Col2 中),所以我想要在 df2.Col2 中填充 NaN 值,其中 df2.Col1==df1.Col1 。 所以我的第二个数据框看起来像:

# df2 :
+------+------+
| Col1 | Col2 |
+------+------+
|   A  |  1   |
|   B  |  2   |
|   D  |  NaN |
+------+------+

我使用的是 Python 2.7

【问题讨论】:

  • 看看 numpy.where。

标签: python pandas dataframe


【解决方案1】:

drop_duplicatesset_indexcombine_first 一起使用:

df = df2.set_index('Col1').combine_first(df1.drop_duplicates().set_index('Col1')).reset_index()

如果只需要检查 id 列中的欺骗:

df = df2.set_index('Col1').combine_first(df1.drop_duplicates().set_index('Col1')).reset_index()

【讨论】:

    【解决方案2】:

    这是一个带有过滤器df1.Col1 == df2.Col1的解决方案

    df2['Col2'] = df1[df1.Col1 == df2.Col1]['Col2']
    

    使用loc 更好(但在我看来不太清楚)

    df2['Col2'] = df1.loc[df1.Col1 == df2.Col2, 'Col2']
    

    【讨论】:

    • 我已经尝试过这个解决方案,但我返回:ValueError: Can only compare samely-labeled Series objects
    • 好的,第 1 列中有两个 A。首先使用 df1.drop_duplicates(inplace=True)
    • 请注意,如果您有 2 个不同的行,例如“A 1”和“A 2”,则删除重复不会删除一个,您仍然会有 ValueError,这是一个很好的想法,因为它不知道哪个要使用的行。
    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多