Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generating a dataframe from a groupby transformation

I have this code:

df.droupby('type)['feature1].mean()

df has 15 features from 1 to 15

I want to iterate over a list of the features names and append these 15 results in a dataframe:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired Output: type(dataframe)

    type      feature1       features2  ..... feature15
    type_A    mean(float)    mean(float)      mean(float)
    type_B    mean(float)    mean(float)      mean(float)
    type_c    mean(float)    mean(float)      mean(float)

What I did:

I have the list of features:

list = df.iloc[:, 10:24].columns.to_list()

and tried something like this:

for i in len(list):
    df.groupby('type')[list[i]].mean()

to see if I get something and this line returns an error:

'int' object is not iterable

Can anyone help me with this?

>Solution :

IIUC, you could simply use groupby + mean:

out = df.groupby('type', as_index=False).mean()

But if you have a bunch of other columns that you don’t want to include in the calculation and only want the mean of "feature…" columns, you could filter, then groupby + mean:

out = df.filter(like='feature').groupby(df['type']).mean().reset_index()

Output:

  type  feature1  feature2
0    A      10.0      11.0
1    B      12.0      14.0
2    C      13.0      13.0
3    D      12.0      19.0
4    E      10.0      10.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading