Home > front end >  csv format of dataframe from pd.concate groupby() dataframe in python
csv format of dataframe from pd.concate groupby() dataframe in python

Time:04-22

Lets say I have multiple data frames that have a format of

Id no A B
1 2 1
2 3 5
2 5 6
1 6 7

which I want to group the data frame by "Id" and apply an aggression then store the new values in a different dataframe such as

df_calc = pd.DataFrame(columns=["Mean", "Median", "Std"])
for df in dataframes:
        mean = df.groupby(["Id"]).mean()
        median = df.groupby(["Id"]).median()
        std = df.groupby(["Id"]).std()

        df_f = pd.DataFrame(
            {"Mean": [mean], "Median": [median], "Std": [std]})

df_calc = pd.concat([df_calc, df_f])

This is the format in which my final dataframe df_calc comes out as
enter image description here

but I would like for it to look like this enter image description here

How do I go about doing so?

CodePudding user response:

You can try agg multiple functions then swap the column level and reorder the column:

out = df.groupby('Id no').agg({'A': ['median','std','mean'],
                               'B': ['median','std','mean']})

print(out)

           A                     B
      median       std mean median       std mean
Id no
1        4.0  2.828427  4.0    4.0  4.242641  4.0
2        4.0  1.414214  4.0    5.5  0.707107  5.5
out = out.swaplevel(0, 1, 1).sort_index(axis=1)

print(out)

      mean      median            std
         A    B      A    B         A         B
Id no
1      4.0  4.0    4.0  4.0  2.828427  4.242641
2      4.0  5.5    4.0  5.5  1.414214  0.707107
  • Related