简体   繁体   English

如何根据 Pandas 中的另一个 DataFrame 更改 DataFrame 特定列中的值

[英]How to change values in certain column of DataFrame based on another DataFrame in Pandas

I have a dataframe called 'data':我有一个名为“数据”的数据框:

USER   FIELD1
Jack        1
Jill        2
Kane        3

and a separate dataframe called 'ids' that serves to be used as a conversion table:以及一个名为“ids”的单独数据框,用作转换表:

ID            ID_NEW
Jack        Jack_NEW
Jill        Jill_NEW
Tyler      TYLER_NEW

How can I iterate through 'data' to replace the USER value if it is found in the 'ids' dataframe while keeping values that are not found?如果在 'ids' 数据框中找到了 USER 值,同时保留未找到的值,我如何遍历 'data' 以替换 USER 值? To end up with something like:最终得到类似的东西:

USER           FIELD1
Jack_NEW            1
Jill_NEW            2
Kane                3

You can do this in a couple steps.您可以通过几个步骤完成此操作。 First, you join the two DataFrames together on "USER = ID" using pd.merge .首先,您使用pd.merge在“USER = ID” 上将两个 DataFrame 连接在一起。 This will leave nulls in "ID_NEW" where the dfs don't join, so then you bring in the "USER" values for those using Series.combine_first .这将在 dfs 不加入的“ID_NEW”中留下空值,因此您可以为使用Series.combine_first 的人引入“USER”值。 Finally, you reassign column names and filter down to just the columns you want to keep.最后,您重新分配列名并过滤到您想要保留的列。

merged = pd.merge(data, ids, how="left", left_on="USER", right_on="ID")
merged["USER"] = merged["ID_NEW"].combine_first(merged["USER"])
data = merged[["USER", "FIELD1"]]

If you have a lot of columns in data , you could do data = merged[data.columns] at the end.如果你有很多列data ,你可以在最后做data = merged[data.columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM