Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group a Series by values in pandas?

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?

like image 644
Martín Fixman Avatar asked Nov 02 '15 17:11

Martín Fixman


People also ask

Can you Groupby on a pandas series?

Group Series using a mapper or by a Series of columns. A groupby operation involves some combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups.

How do you group values in pandas?

The Hello, World! of pandas GroupBy You call . groupby() and pass the name of the column that you want to group on, which is "state" . Then, you use ["last_name"] to specify the columns on which you want to perform the actual aggregation. You can pass a lot more than just a single column name to .

What is group by () in pandas library?

Pandas groupby is used for grouping the data according to the categories and apply a function to the categories. It also helps to aggregate data efficiently. Pandas dataframe. groupby() function is used to split the data into groups based on some criteria.

How do I get Groupby columns in pandas?

You can also reset_index() on your groupby result to get back a dataframe with the name column now accessible. If you perform an operation on a single column the return will be a series with multiindex and you can simply apply pd. DataFrame to it and then reset_index. Show activity on this post.


2 Answers

grouped = s.groupby(s) 

Or:

grouped = s.groupby(lambda x: s[x]) 
like image 158
luca Avatar answered Oct 03 '22 04:10

luca


Three methods:

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

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

Series to DataFrame:

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

like image 27
Hangyu Liu Avatar answered Oct 03 '22 04:10

Hangyu Liu