[英]Replace column A's value based on column B's max
Input:输入:
ID number account date
1 00002 GA1 1/1/2021
1 00004 GA1 1/3/2021
2 00001 GA1 1/4/2021
3 00012 GA2 1/3/2021
4 00010 GA2 1/2/2021
Output: Output:
ID number account date
1 00002 GA1 1/1/2021
1 00002 GA1 1/1/2021
1 00002 GA1 1/1/2021
4 00010 GA2 1/2/2021
4 00010 GA2 1/2/2021
Rules:规则:
I have done the first part of the query -我已经完成了查询的第一部分 -
df.groupby(['account'])['date'].transform('min')
However, I am having trouble transform the ID and number columns based on the date column.但是,我无法根据日期列转换 ID 和数字列。 I'm looking for an efficient way to do this.我正在寻找一种有效的方法来做到这一点。
Sorting by account and date then taking the first row of each group would give what you require.按帐户和日期排序,然后取每个组的第一行将给出您需要的内容。
The next step is to join it back to the original DataFrame下一步就是把它加入原来的DataFrame
In [18]: df[['account']].join(df.sort_values(['account', 'date']).groupby('account').first(), on='account')
Out[18]:
account ID number date
0 GA1 1 2 1/1/2021
1 GA1 1 2 1/1/2021
2 GA1 1 2 1/1/2021
3 GA2 4 10 1/2/2021
4 GA2 4 10 1/2/2021
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.