How to group a Series by values in pandas?

后端 未结 5 1927
悲&欢浪女
悲&欢浪女 2021-02-01 00:31

I currently have a pandas Series with dtype Timestamp, and I want to group it by date (and have many rows with different times in each group).

5条回答
  •  情深已故
    2021-02-01 01:00

    You should convert it to a DataFrame, then add a column that is the date(). You can do groupby on the DataFrame with the date column.

    df = pandas.DataFrame(s, columns=["datetime"])
    df["date"] = df["datetime"].apply(lambda x: x.date())
    df.groupby("date")
    

    Then "date" becomes your index. You have to do it this way because the final grouped object needs an index so you can do things like select a group.

提交回复
热议问题