![](/img/trans.png)
[英]replace value of a dataframe based on value from another dataframe
[英]Replace value in a dataframe with a value from another dataframe
我正在寫關於英超聯賽的碩士論文,但我遇到了一個問題。
>Chelsea
Attacking duo HT Attacking duo AT HomeTeam AwayTeam
X Yes Liverpool Chelsea
No X Chelsea Manchester Utd
X No Arsenal Chelsea
No X Chelsea Norwich
>Liverpool
Attacking duo HT Attacking duo AT HomeTeam AwayTeam
No X Liverpool Manchester City
X Yes Burnley Liverpool
No X Liverpool Chelsea
X Yes Arsenal Liverpool
在這個例子中,我想知道如何將切爾西 dataframe 第一行中的切爾西 X 值替換為利物浦第三行 dataframe 中的相應值('否')
您可以使用df.update
將NaN
替換為來自另一個 df 的non-NAN
首先將X
s 更改為NaN
s 並將HomeTeam-AwayTeam
設置為索引
import numpy as np # if you haven't already
for df in (Chelsea, Liverpool):
(df
.replace('X', np.nan, inplace=True)
.set_index(['HomeTeam', 'AwayTeam'], inplace=True) # so pandas can align the rows the way we need them
)
現在你可以更新了
Chelsea.update(Liverpool)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.