gpt4 book ai didi

python Pandas : Keeping earliest data in new column when grouping data

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:19 25 4
gpt4 key购买 nike

我正在尝试按列分组,找到该组中的最小日期值并将其插入到该组中所有值的新列中。

以下内容:

d = {'one' : pd.Series(np.random.randn(6), index=pd.date_range('1/1/2011', periods=6, freq='H')),
'two' : pd.Series(["A", "B", "C"] * 2, index=pd.date_range('1/1/2011', periods=6, freq='H'))}
df = pd.DataFrame(d)
df['ts'] = df.index
df['min_date'] = df.groupby('two')['ts'].min()
df

给我这个输出:

Out[7]: 
one two ts min_date
2011-01-01 00:00:00 1.676829 A 2011-01-01 00:00:00 NaT
2011-01-01 01:00:00 -0.490976 B 2011-01-01 01:00:00 NaT
2011-01-01 02:00:00 -1.934902 C 2011-01-01 02:00:00 NaT
2011-01-01 03:00:00 -0.625931 A 2011-01-01 03:00:00 NaT
2011-01-01 04:00:00 1.534645 B 2011-01-01 04:00:00 NaT
2011-01-01 05:00:00 0.123045 C 2011-01-01 05:00:00 NaT

[6 rows x 4 columns]

我想要的地方:

Out[7]: 
one two ts min_date
2011-01-01 00:00:00 1.676829 A 2011-01-01 00:00:00 2011-01-01 00:00:00
2011-01-01 01:00:00 -0.490976 B 2011-01-01 01:00:00 2011-01-01 01:00:00
2011-01-01 02:00:00 -1.934902 C 2011-01-01 02:00:00 2011-01-01 02:00:00
2011-01-01 03:00:00 -0.625931 A 2011-01-01 03:00:00 2011-01-01 00:00:00
2011-01-01 04:00:00 1.534645 B 2011-01-01 04:00:00 2011-01-01 01:00:00
2011-01-01 05:00:00 0.123045 C 2011-01-01 05:00:00 2011-01-01 02:00:00

[6 rows x 4 columns]

two 列上分组,因此 A 的第一次出现设置在所有 A 条目的 min_date 中,等等。

最佳答案

我想你想要 transform方法:

>>> df['min_date'] = df.groupby('two')['ts'].transform("min")
>>> df
one two ts min_date
2011-01-01 00:00:00 0.574285 A 2011-01-01 00:00:00 2011-01-01 00:00:00
2011-01-01 01:00:00 -0.200439 B 2011-01-01 01:00:00 2011-01-01 01:00:00
2011-01-01 02:00:00 0.549725 C 2011-01-01 02:00:00 2011-01-01 02:00:00
2011-01-01 03:00:00 1.187299 A 2011-01-01 03:00:00 2011-01-01 00:00:00
2011-01-01 04:00:00 0.770180 B 2011-01-01 04:00:00 2011-01-01 01:00:00
2011-01-01 05:00:00 -0.448781 C 2011-01-01 05:00:00 2011-01-01 02:00:00

[6 rows x 4 columns]

它用于执行聚合操作,然后将结果广播到整个组。

关于 python Pandas : Keeping earliest data in new column when grouping data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23093664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com