简体   繁体   English

如何从 dataframe 中获取 map 值并获取最后更新

[英]How to map values from a dataframe and get the last update

I'm stuck with a problem because map() don't let me map values if they are repeated.我遇到了一个问题,因为 map() 不让我重复 map 值。 The scenario is this.场景是这样的。

I have this df1 of daily observations我有这个每日观察的df1

   Obvserved_Today  A   B  
0  192.168.1.1      3   2
1  192.168.1.3      1   3
2  192.168.1.4      1   5
3  192.168.1.5      2   9

I want to update that df by adding two columns of historical data, so I already have this second dataframe df2 of historics我想通过添加两列历史数据来更新该 df,所以我已经有了第二个 dataframe df2 的历史数据

   IP_Historical    Ticket  Date  
0  192.168.1.1      3001    11/01/2020
1  192.168.1.1      3002    11/02/2020
2  192.168.1.3      3003    11/03/2020
3  192.168.1.5      3004    11/04/2020
4  192.168.1.5      3005    11/05/2020

I want an output like the one below, I want to add two more columns, the first one containing the last ticket seen in the historical df2 and a second one, also from the historical df2, with the last seen date if there's no match let it as NaN or 0.我想要一个像下面这样的 output,我想再添加两列,第一列包含历史 df2 中看到的最后一张票,第二张包含历史 df2 中的最后一张票,如果没有匹配项,则为最后看到的日期它为 NaN 或 0。

   Obvserved_Today  A   B   Last Ticket  Last update
0  192.168.1.1      3   2   3002         11/02/2020
1  192.168.1.3      1   3   3003         11/03/2020
2  192.168.1.4      1   5   0            0
3  192.168.1.5      2   9   3005         11/05/2020

df1.merge(df2[~df2.duplicated('Obvserved_Today',keep='last')], how='left', on='Obvserved_Today')

This will have the columns named "Ticket" instead of "Last Ticket" and "Date" instead of "Last update".这将具有名为“Ticket”而不是“Last Ticket”和“Date”而不是“Last update”的列。 If you want you can rename after the merge.如果需要,您可以在合并后重命名。

What is this code doing?这段代码在做什么?

It is merging the two dataframe (looking at the on= column and matching up same values) but only looking at the rows of the second dataframe that are the last of the observation.它正在合并两个 dataframe(查看on=列并匹配相同的值),但只查看第二个 dataframe 的行,这是观察的最后一个。

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

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