我目前有一个大熊猫Series
与dtype Timestamp
,我想按日期分组(并在每个组中有不同时间的许多行)。如何通过pandas中的值分组系列?
这样做,这将是类似的东西,以
grouped = s.groupby(lambda x: x.date())
然而看似显而易见的方法,大熊猫groupby
组系列通过其索引。我怎样才能让它按价值分组?
我目前有一个大熊猫Series
与dtype Timestamp
,我想按日期分组(并在每个组中有不同时间的许多行)。如何通过pandas中的值分组系列?
这样做,这将是类似的东西,以
grouped = s.groupby(lambda x: x.date())
然而看似显而易见的方法,大熊猫groupby
组系列通过其索引。我怎样才能让它按价值分组?
您应该将其转换为DataFrame,然后添加一个为date()的列。您可以使用日期列在DataFrame上进行groupby。
df = pandas.DataFrame(s, columns=["datetime"])
df["date"] = df["datetime"].apply(lambda x: x.date())
df.groupby("date")
然后“日期”成为您的索引。你必须这样做,因为最终的分组对象需要一个索引,所以你可以做像select a group这样的事情。
我用这个由它的值grouby一个系列:
grouped = s.groupby(s)
或者:
grouped = s.groupby(lambda x: s[x])
这么简单...谢谢:) –
有三种方法:
数据框:pd.groupby(['column']).size()
系列:sel.groupby(sel).size()
系列到数据帧:
pd.DataFrame(sel, columns=['column']).groupby(['column']).size()
IIUC不'分组= s.groupby(s.dt.date)'工作?你试图实现什么 – EdChum
http://stackoverflow.com/questions/17929426/groupby-for-pandas-series-not-working – luca