簡體   English   中英

Python Pandas使用基於另一個具有重疊索引的數據框中的列的值來更新數據框列

[英]Python Pandas Update a Dataframe Column with a Value based on a Column in Another Dataframe with Overlapping Indices

這可能有一個簡單的答案,但是以某種方式我看不到它。

我有兩個數據df_adf_b df_b.index的一個子集df_a.index

df_a

              Actioncode   Group

    Mary         1.0         I
    Paul         1.0         I
    Robert       4.0         O
    David        4.0         O
    Julia        4.0         O

請注意, Group與一個ActionCode (只需使該Actioncode可讀即可。

df_b

              Group

    Paul        O
    Robert      I

我要的是df_a Actioncode顯示5.0如果名稱是df_bGroup是“O”和df_a Actioncode顯示3.0如果名稱是df_bGroup是“我”。

因此結果將是:

    df_a

              Actioncode   Group

    Mary         1.0         I
    Paul         5.0         I
    Robert       3.0         O
    David        4.0         O
    Julia        4.0         O

我在where嘗試過where但似乎無法理解。

df_a['Actioncode'] =  df_a['Actioncode'].where(df_b['Group'] == 'O', 5.0)

但這並不完全正確。

我可以迭代,但不是pythonic。

見解?

謝謝,

您可以為此使用np.select ,它的工作方式類似於np.where但是具有多個條件/輸出:

# Transform index of df_a to series for mapping
a_idx = df_a.index.to_series()

# Condition that df_a's index is in df_b
idx_in = a_idx.isin(df_b.index)

# map df_a's index to the df_b groups
mapped = a_idx.map(df_b.Group)

# apply np.select on your conditions:
conds = [(idx_in) & (mapped == 'O'),
         (idx_in) & (mapped == 'I')]

choices = [5,3]


df_a['Actioncode'] = np.select(conds,choices, df_a.Actioncode)

>>> df_a
        Actioncode Group
Mary           1.0     I
Paul           5.0     I
Robert         3.0     O
David          4.0     O
Julia          4.0     O

np.where和映射的另一個選項。

scores = pd.Series(df_a.index).map(df_b['Group'].map({'O': 5.0, 'I': 3.0}))
df_a['Actioncode'] = np.where(scores.isnull(), df_a['Actioncode'], scores)

細節:

>>> df_a
        Actioncode Group
Mary           1.0     I
Paul           1.0     I
Robert         4.0     O
David          4.0     O
Julia          4.0     O
>>> scores = pd.Series(df_a.index).map(df_b['Group'].map({'O': 5.0, 'I': 3.0}))
>>> scores
0    NaN
1    5.0
2    3.0
3    NaN
4    NaN
dtype: float64
>>> 
>>> where = np.where(scores.isnull(), df_a['Actioncode'], scores)
>>> where
array([1., 5., 3., 4., 4.])
>>>
>>> df_a['Actioncode'] = where
>>> df_a
        Actioncode Group
Mary           1.0     I
Paul           5.0     I
Robert         3.0     O
David          4.0     O
Julia          4.0     O

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM