Source Code:
(back to article)
import pandas as pd # Create a sample DataFrame df = pd.DataFrame({'A': ['foo', 'bar', 'baz', 'foo', 'bar', 'baz'], 'B': [1, 2, 3, 4, 5, 6], 'C': [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]}) # Perform a groupby operation on column 'A' grouped = df.groupby('A') # Get the mean of the groups mean_values = grouped.mean() # Convert the Series to a DataFrame mean_df = pd.DataFrame(mean_values)
Result:
Report an issue