Aggregating groups results in pandas dataframe


Hani Ihlayyle

I'm just wondering how to aggregate all the results into a place grouped by a pandas dataframe.

data1 = {'id':['1', '1', '2', '2', '2', '3', '3', '3'],  
        'Age':[27, 24, 22, 32, 33, 36, 27, 32],    
        'Qualification':['Msc', 'MA', 'MCA', 'Phd','B.Tech', 'B.com', 'Msc', 'MA']
        }
df = pd.DataFrame(data1) 


grp = df.groupby('id') 
for name, group in grp:
    x = group.Age * 2 
    print (x)

0    54
1    48
Name: Age, dtype: int64
2    44
3    64
4    66
Name: Age, dtype: int64
5    72
6    54
7    64
Name: Age, dtype: int64

I'm tired like this and only save the last set.

result = pd.DataFrame(x) 
result

Age
5   72
6   54
7   64

How can I save all these results in pandas dataframe?

expected grades

Age 
0   54
1   48
2   44
3   64
4   66
5   72
6   54
7   64
shan mountain
data1 = {'id':['1', '1', '2', '2', '2', '3', '3', '3'],  
         'Age':[27, 24, 22, 32, 33, 36, 27, 32],    
         'Qualification':['Msc', 'MA', 'MCA', 'Phd','B.Tech', 'B.com', 'Msc', 'MA']
        }
df = pd.DataFrame(data1) 

grp = df.groupby('id') 
x = []    # Create an empty list

for name, group in grp:
   b = group.Age * 2  # Do your group vise operations 
   x.extend(b)               # Extend your list with with your restuls

result = pd.DataFrame(x, columns=['Age'])
print(result)


    Age
0   54
1   48
2   44
3   64
4   66
5   72
6   54
7   64

Related


Aggregating groups results in pandas dataframe

Hani Ihlayyle I'm just wondering how to aggregate all the results into a place grouped by a pandas dataframe. data1 = {'id':['1', '1', '2', '2', '2', '3', '3', '3'], 'Age':[27, 24, 22, 32, 33, 36, 27, 32], 'Qualification':['Msc', 'MA', 'M

Numbered groups in Pandas DataFrame

Gree Tree Python In Pandas, is there a way to number groups in a DataFrame based on column values? If my frame looks like this Column1 Column2 Column3 0 A X 23 1 A X 45 2 A Y 32 3 A Y 5

Pandas DataFrame for *interleaved* groups

bat-like I have a DataFrames data on 3 axes with membership labels for grouping: df = pd.DataFrame( [[0, 1, 2, 0], [-1, 0, 1, 0], [-2, 0, 3, 1], [1, 1, 3, 1], [1, 0, 2, 2],

Pandas DataFrame for *interleaved* groups

bat-like I have a DataFrames data on 3 axes with membership labels for grouping: df = pd.DataFrame( [[0, 1, 2, 0], [-1, 0, 1, 0], [-2, 0, 3, 1], [1, 1, 3, 1], [1, 0, 2, 2],

Pandas DataFrame for *interleaved* groups

bat-like I have a DataFrames data on 3 axes with membership labels for grouping: df = pd.DataFrame( [[0, 1, 2, 0], [-1, 0, 1, 0], [-2, 0, 3, 1], [1, 1, 3, 1], [1, 0, 2, 2],

Pandas DataFrame for *interleaved* groups

bat-like I have a DataFrames data on 3 axes with membership labels for grouping: df = pd.DataFrame( [[0, 1, 2, 0], [-1, 0, 1, 0], [-2, 0, 3, 1], [1, 1, 3, 1], [1, 0, 2, 2],

Pandas DataFrame for *interleaved* groups

bat-like I have a DataFrames data on 3 axes with membership labels for grouping: df = pd.DataFrame( [[0, 1, 2, 0], [-1, 0, 1, 0], [-2, 0, 3, 1], [1, 1, 3, 1], [1, 0, 2, 2],

Pandas DataFrame for *interleaved* groups

bat-like I have a DataFrames data on 3 axes with membership labels for grouping: df = pd.DataFrame( [[0, 1, 2, 0], [-1, 0, 1, 0], [-2, 0, 3, 1], [1, 1, 3, 1], [1, 0, 2, 2],

Delete groups by array of row groups in pandas dataframe

Aliga I need to remove all markers in a group with less than 3 rows. Dataframe:- token positive 0 58 1 1 58 8 2 63 5 3 63 9 4 63 0 5 97 6 6 97 1 I filter groups with less than 3 rows, but how do I remove these groups from the main dataframe? c_df = df.groupby(

Can't sort values after aggregating with Pandas dataframe

Pedro Alves I have the following dataframe: df[['ID','Team']].groupby(['Team']).agg([('total','count')]).reset_index("total").sort_values("count") I basically need to count the number of IDs by team and then sort by the total number of IDs. The aggregation pa

Can't sort values after aggregating with Pandas dataframe

Pedro Alves I have the following dataframe: df[['ID','Team']].groupby(['Team']).agg([('total','count')]).reset_index("total").sort_values("count") I basically need to count the number of IDs by team and then sort by the total number of IDs. The aggregation pa

Can't sort values after aggregating with Pandas dataframe

Pedro Alves I have the following dataframe: df[['ID','Team']].groupby(['Team']).agg([('total','count')]).reset_index("total").sort_values("count") I basically need to count the number of IDs by team and then sort by the total number of IDs. The aggregation pa

Can't sort values after aggregating with Pandas dataframe

Pedro Alves I have the following dataframe: df[['ID','Team']].groupby(['Team']).agg([('total','count')]).reset_index("total").sort_values("count") I basically need to count the number of IDs by team and then sort by the total number of IDs. The aggregation pa

Can't sort values after aggregating with Pandas dataframe

Pedro Alves I have the following dataframe: df[['ID','Team']].groupby(['Team']).agg([('total','count')]).reset_index("total").sort_values("count") I basically need to count the number of IDs by team and then sort by the total number of IDs. The aggregation pa

Split Pandas Dataframe into different groups

Carlo Aloka Given the following dataset DF: uuid,eventTime,Op.progress,Op.progressPercentage, AnotherAttribute C0972765-8436-0000-0000-000000000000,2017-08-19T12:52:39,P,3.0,01:57:00 C0972765-8436-0000-0000-000000000000,2017-08-19T12:52:49,P,3.0,01:56:00 C0972

Add rows to groups in Pandas DataFrame

mechanical I have the following Pandas DataFrame: start_timestamp_milli end_timestamp_milli name rating 1 1555414708025 1555414723279 Valence 2 2 1555414708025 1555414723279 Arousal 6 3

Add rows to groups in Pandas DataFrame

mechanical I have the following Pandas DataFrame: start_timestamp_milli end_timestamp_milli name rating 1 1555414708025 1555414723279 Valence 2 2 1555414708025 1555414723279 Arousal 6 3

Summarize groups of columns in pandas dataframe

Nizag I have a pandas dataframe with 600 columns (df1) and I want to sum the values of each column in groups of 6. In other words, I want to create a new dataframe (df2) with 100 columns, each column is the sum of the 6 columns in the input dataframe. For exam

Pandas: add rows to groups in a dataframe

Pratyusha Pasumarty I have a dataframe like this: df = pd.DataFrame({"date": [1,2,5,6,2,3,4,5,1,3,4,5,6,1,2,3,4,5,6], "variable": ["A","A","A","A","B","B","B","B","C","C","C","C","C","D","D","D","D","D","D"]}) date variable 0 1 A 1 2

Summarize groups of columns in pandas dataframe

Nizag I have a pandas dataframe with 600 columns (df1) and I want to sum the values of each column in groups of 6. In other words, I want to create a new dataframe (df2) with 100 columns, each column is the sum of the 6 columns in the input dataframe. For exam

Add rows to groups in Pandas DataFrame

username I have a pandas dataframe df. Create it like this: a = np.array([0,0,0,1,1,1,2,2,2]).T bcd = np.array([np.arange(1,10)]*3).T df = pd.DataFrame(bcd, columns=["b","c","d"]) df["a"] = a It looks like this: b c d a 0 1 1 1 0 1 2

Pandas: add rows to groups in a dataframe

Pratyusha Pasumarty I have a dataframe like this: df = pd.DataFrame({"date": [1,2,5,6,2,3,4,5,1,3,4,5,6,1,2,3,4,5,6], "variable": ["A","A","A","A","B","B","B","B","C","C","C","C","C","D","D","D","D","D","D"]}) date variable 0 1 A 1 2