如何在pandas中用其他列替换列值?

huangapple go评论114阅读模式
英文:

How to replace column value with other columns in pandas?

问题

我有一个简单的问题。我有两个数据框:

df1 =

code countries
FR 法国
US 美国
IT 意大利

和另一个数据框是:

df2=

countries
FR
FR
IT
US
US
US
IT

我想在df2中用df1中的国家列来替换。

英文:

I have a simple question. I have 2 dataframe:

df1 =

code countries
FR France
US United-States
IT Italy

and an other one which is:

df2=

countries
FR
FR
IT
US
US
US
IT

I would like in df2 replace the countries column with the one in df1.

答案1

得分: 1

与df1上的代码合并,并与df2上的国家合并,稍微进行重命名,然后只获取国家列。

  1. df2 = df2.merge(df1, right_on='code', left_on='countries', suffixes=('_old', ''))
  2. df2 = df2[['countries']]
英文:

Merge with code on df1 and countries on df2 with a bit of renaming and then just grab the countries column.

  1. df2 = df2.merge(df1, right_on = 'code', left_on = 'countries', suffixes = ('_old', ''))
  2. df2 = df2[['countries']]

答案2

得分: 1

  1. 使用 `df1` `dict` 形式对数值进行映射:

>>> df2["countries"].map(df1.set_index("code").squeeze().to_dict())

0 法国
1 法国
2 意大利
3 美国
4 美国
5 美国
6 意大利
Name: countries, dtype: object

  1. <details>
  2. <summary>英文:</summary>
  3. `map` the values using the `dict` form of `df1`:

>>> df2["countries"].map(df1.set_index("code").squeeze().to_dict())

0 France
1 France
2 Italy
3 United-States
4 United-States
5 United-States
6 Italy
Name: countries, dtype: object

  1. </details>
  2. # 答案3
  3. **得分**: 0
  4. 你可以使用 `merge` 函数,然后重命名和删除多余的列。
  5. 我添加了一些额外的操作以在合并后还原索引。
  6. ```python
  7. df1 = pd.DataFrame({
  8. "code": ["FR", "US", "IT"],
  9. "countries": ["France", "United-States", "Italy"]
  10. })
  11. df2 = pd.DataFrame({
  12. "countries": ['FR', 'FR', 'IT', 'US', 'US', 'US', 'IT'],
  13. "idx": range(7),
  14. })
  15. df2.reset_index(inplace=True)
  16. df2 \
  17. .merge(df1, left_on="countries", right_on="code") \
  18. .rename({"countries_y": "countries"}, axis=1) \
  19. .set_index("index") \
  20. .drop(["code", "countries_x"], axis=1)

输出:

  1. countries
  2. index
  3. 0 France
  4. 1 France
  5. 2 Italy
  6. 6 Italy
  7. 3 United-States
  8. 4 United-States
  9. 5 United-States
英文:

You can use merge function, then rename and drop extra columns.
I added some extra actions with index to restore it after merging.

  1. df1 = pd.DataFrame({
  2. &quot;code&quot;: [&quot;FR&quot;, &quot;US&quot;, &quot;IT&quot;],
  3. &quot;countries&quot;: [&quot;France&quot;, &quot;United-States&quot;, &quot;Italy&quot;]
  4. })
  5. df2 = pd.DataFrame({
  6. &quot;countries&quot;: [&#39;FR&#39;, &#39;FR&#39;, &#39;IT&#39;, &#39;US&#39;, &#39;US&#39;, &#39;US&#39;, &#39;IT&#39;],
  7. &quot;idx&quot;: range(7),
  8. })
  9. df2.reset_index(inplace=True)
  10. df2 \
  11. .merge(df1, left_on=&quot;countries&quot;, right_on=&quot;code&quot;) \
  12. .rename({&quot;countries_y&quot;: &quot;countries&quot;}, axis=1) \
  13. .set_index(&quot;index&quot;) \
  14. .drop([&quot;code&quot;, &quot;countries_x&quot;], axis=1)

Output:

  1. countries
  2. index
  3. 0 France
  4. 1 France
  5. 2 Italy
  6. 6 Italy
  7. 3 United-States
  8. 4 United-States
  9. 5 United-States

huangapple
  • 本文由 发表于 2023年7月18日 01:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定