Search code examples
pythonpandasgroup-bymean

Using python to calculate average of a grouped column


I have written a Python code to average numbers by groups. Hereby I want to average 5th column grouped by 2nd column.

import numpy as np
import sys
import pandas as pd
df = pd.read_table("test.txt", delimiter=" ")
result = df.groupby(['radii']).mean()
np.savetxt('result.txt', result.values, fmt='%f')

My data is:

no radii len num dens

1 0.0246136 3.9 0.0006 1.04692

2 0.0246136 5.9 0.0002 0.348973

3 0.0738409 7.9 0.0018 1.04692

4 0.0738409 9.9 0.0012 0.697945

5 0.123068 3.9 0.0014 0.488562

6 0.123068 5.9 0.0016 0.558356

But, unfortunately I do not get the second column in the output result (see below): output result is:

1.500000 4.900000 0.000400 0.697947

3.500000 8.900000 0.001500 0.872432

5.500000 4.900000 0.001500 0.523459

My expected result is:

0.0246136 0.6979465

0.0738409 0.8724325

0.123068 0.523459

Thanks for your comments


Solution

  • If I understand you correctly, you need to add

    result.reset_index(inplace=True)
    

    The reason is that the column you're grouping by becomes the index. By resetting the index, the groupby column will become a column in the dataframe.