How to group a Series by values in pandas?

PythonPandasGroup BySeries

Python Problem Overview


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).

The seemingly obvious way of doing this would be something similar to

grouped = s.groupby(lambda x: x.date())

However, pandas' groupby groups Series by its index. How can I make it group by value instead?

Python Solutions


Solution 1 - Python

grouped = s.groupby(s)

Or:

grouped = s.groupby(lambda x: s[x])

Solution 2 - Python

Three methods:

DataFrame: pd.groupby(['column']).size()

Series: sel.groupby(sel).size()

Series to DataFrame:

pd.DataFrame( sel, columns=['column']).groupby(['column']).size()

Solution 3 - Python

For anyone else who wants to do this inline without throwing a lambda in (which tends to kill performance):

s.to_frame(0).groupby(0)[0]

Solution 4 - Python

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.

Solution 5 - Python

To add another suggestion, I often use the following as it uses simple logic:

pd.Series(index=s.values).groupby(level=0)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartín FixmanView Question on Stackoverflow
Solution 1 - PythonlucaView Answer on Stackoverflow
Solution 2 - PythonHangyu LiuView Answer on Stackoverflow
Solution 3 - PythonAndy JonesView Answer on Stackoverflow
Solution 4 - PythonmirthbottleView Answer on Stackoverflow
Solution 5 - Pythonmchl_kView Answer on Stackoverflow