简体   繁体   English

合并和替换值 dataframe pandas

[英]combine and replace value dataframe pandas

I have two dataframes with the same date and client id, but with a different amount.我有两个具有相同日期和客户端 ID 的数据框,但数量不同。

I try to get another dataframe with dfA amount value and keep the another 0's on dfB when dfA does not exist我尝试使用 dfA 金额值获取另一个 dataframe 并在 dfA 不存在时在 dfB 上保留另一个 0

dfA:
    client_id  date         amount
0     1        2020-07-11    100
1     1        2020-07-10    90
2     1        2020-07-09    80
3     1        2020-07-12    70
3     1        2020-07-01    86

dfB:
    client_id  date         amount
0     1        2020-07-11    0
1     1        2020-07-10    0
2     1        2020-07-09    0
3     1        2020-07-07    0
4     1        2020-07-06    0
5     1        2020-07-05    0
5     1        2020-07-04    0
3     1        2020-07-03    0
4     1        2020-07-02    0
5     1        2020-07-01    0

I want to get:我想得到:

dfResult:
    client_id  date         amount
0     1        2020-07-11    100
1     1        2020-07-10    90
2     1        2020-07-09    80
3     1        2020-07-07    70
4     1        2020-07-06    0
5     1        2020-07-05    0
5     1        2020-07-04    0
3     1        2020-07-03    0
4     1        2020-07-02    0
5     1        2020-07-01    86

You can concat the df's together, sort by amount and then drop duplicates.您可以将concat连接在一起,按数量排序,然后删除重复项。

dfResult = pd.concat([dfA,dfB]).sort_values(by='amout',ascending = False).drop_duplicates(subset=['client_id','date'],keep='first').reset_index().sort_values(by=['client id','date'],ascending = (True,False))

try this,尝试这个,

(
    dfB.date.map(
        dfA.set_index('date')['amount'].to_dict()
    ).fillna(0.0)
)

Or或者

(
    dfB.merge(
        dfA, on=['client_id', 'date'], suffixes=("_x", ""), how='left'
    ).fillna(0.0).drop(columns=["amount_x"])
)

   client_id        date  amount
0          1  2020-07-11  100.0
1          1  2020-07-10   90.0
2          1  2020-07-09   80.0
3          1  2020-07-07    0.0
4          1  2020-07-06    0.0
5          1  2020-07-05    0.0
5          1  2020-07-04    0.0
3          1  2020-07-03    0.0
4          1  2020-07-02    0.0
5          1  2020-07-01   86.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM