Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一些包含日期的数据,例如:
1979-02-15 1979-02-15 1979-02-17 1979-02-17
我想按年、月和日期对数据进行分组,以便数据看起来像
1979 02 15 1979-02-15 1979-02-15 1979 02 17 1979-02-17 1979-02-17
我找到了功能
grouped = df.groupby(lambda x: x.year)
但这仅允许按年份分组。所以,我的问题是如何在熊猫中按日期进行多级分组?
您可以将多个键作为列表传递给 groupby:
from pandas import * from numpy.random import randn rng = date_range('1/1/2011', periods=7200, freq='H') ts = Series(randn(len(rng)), index=rng) for key, data in ts.groupby([rng.year, rng.month]): print key, data.sum()