Aggregation in Pandas

Question 1 How can I perform aggregation with Pandas? Expanded aggregation documentation. Aggregating functions are the ones that reduce the dimension of the returned objects. It means output Series/DataFrame have less or same rows like original. Some common aggregating functions are tabulated below: Function Description mean() Compute mean of groups sum() Compute sum of group … Read more

Concatenate strings from several rows using Pandas groupby

You can groupby the ‘name’ and ‘month’ columns, then call transform which will return data aligned to the original df and apply a lambda where we join the text entries: In [119]: df[‘text’] = df[[‘name’,’text’,’month’]].groupby([‘name’,’month’])[‘text’].transform(lambda x: ‘,’.join(x)) df[[‘name’,’text’,’month’]].drop_duplicates() Out[119]: name text month 0 name1 hej,du 11 2 name1 aj,oj 12 4 name2 fin,katt 11 6 … Read more

How do I create a new column from the output of pandas groupby().sum()?

You want to use transform this will return a Series with the index aligned to the df so you can then add it as a new column: In [74]: df = pd.DataFrame({‘Date’: [‘2015-05-08’, ‘2015-05-07’, ‘2015-05-06’, ‘2015-05-05’, ‘2015-05-08’, ‘2015-05-07’, ‘2015-05-06’, ‘2015-05-05’], ‘Sym’: [‘aapl’, ‘aapl’, ‘aapl’, ‘aapl’, ‘aaww’, ‘aaww’, ‘aaww’, ‘aaww’], ‘Data2’: [11, 8, 10, 15, 110, … Read more

Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

Quick Answer: The simplest way to get row counts per group is by calling .size(), which returns a Series: df.groupby([‘col1′,’col2’]).size() Usually you want this result as a DataFrame (instead of a Series) so you can do: df.groupby([‘col1’, ‘col2’]).size().reset_index(name=”counts”) If you want to find out how to calculate the row counts and other statistics for each … Read more