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

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

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上的国家合并,稍微进行重命名,然后只获取国家列。

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

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

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

答案2

得分: 1

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

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

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


<details>
<summary>英文:</summary>

`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


</details>



# 答案3
**得分**: 0

你可以使用 `merge` 函数,然后重命名和删除多余的列。
我添加了一些额外的操作以在合并后还原索引。

```python
df1 = pd.DataFrame({
    "code": ["FR", "US", "IT"],
    "countries": ["France", "United-States", "Italy"]
})

df2 = pd.DataFrame({
    "countries": ['FR', 'FR', 'IT', 'US', 'US', 'US', 'IT'],
    "idx": range(7),
})

df2.reset_index(inplace=True)

df2 \
    .merge(df1, left_on="countries", right_on="code") \
    .rename({"countries_y": "countries"}, axis=1) \
    .set_index("index") \
    .drop(["code", "countries_x"], axis=1)

输出:

           countries
index               
0             France
1             France
2              Italy
6              Italy
3      United-States
4      United-States
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.

df1 = pd.DataFrame({
    &quot;code&quot;: [&quot;FR&quot;, &quot;US&quot;, &quot;IT&quot;],
    &quot;countries&quot;: [&quot;France&quot;, &quot;United-States&quot;, &quot;Italy&quot;]
})

df2 = pd.DataFrame({
    &quot;countries&quot;: [&#39;FR&#39;, &#39;FR&#39;, &#39;IT&#39;, &#39;US&#39;, &#39;US&#39;, &#39;US&#39;, &#39;IT&#39;],
    &quot;idx&quot;: range(7),
})

df2.reset_index(inplace=True)

df2 \
    .merge(df1, left_on=&quot;countries&quot;, right_on=&quot;code&quot;) \
    .rename({&quot;countries_y&quot;: &quot;countries&quot;}, axis=1) \
    .set_index(&quot;index&quot;) \
    .drop([&quot;code&quot;, &quot;countries_x&quot;], axis=1)

Output:

           countries
index               
0             France
1             France
2              Italy
6              Italy
3      United-States
4      United-States
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:

确定