简体   繁体   English

根据 B 列的最大值替换 A 列的值

[英]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:规则:

  1. For the same account, replace all dates with the earliest date of that account对于同一帐户,将所有日期替换为该帐户的最早日期
  2. For the same account, replace all ID and number with the ID and number where the earliest date appears对于同一账号,将所有ID和号码替换为最早出现日期的ID和号码

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.

相关问题 如何在 dataframe 的 A 列中找到 B 列中的 dataframe 值,如果是,将 B 列中的值替换为 A 列的值? - How do I find in dataframe value in column B exists in Column A in a dataframe, and if so, replace the value in column B with Column A's value? 根据另一列中的值,用字符串替换一列中的NaN - Replace NaN's in one column with string, based on value in another column 如何根据另一列中的值用另一列的平均值替换 NaN 值? Pandas - How to replace NaN values with another column's mean based on value in another column? Pandas 按列 ('tenant') 分组并获取 ('value') 列中的最大连续 1 - Group by a column ('tenant') and get the max consecutive 1s in ('value') column 用 Pandas 的列的平均值替换值 - Replace value with the average of it's column with Pandas 根据另一列的值替换Pandas数据框的Column的值 - Replace values of a Pandas dataframe's Column based on values of another column 基于同一列的先前值对列值进行矢量化计算? - Vectorized calculation of a column's value based on a previous value of the same column? 列值根据另一列的值而变化 - Column value changes based on anoother column's value A列值基于B列的groupby聚合 - Column A value based on groupby aggregation of Column B 根据列值复制 dataframe 行 - replicate dataframe rows based on a column's value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM