繁体   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