from tabulate import tabulate import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda', 'Phil', 'Lucy'],
'Age': [28, 24, 35, 32, 36, 25],
'Country': ['USA', 'UK', 'Australia', 'Germany', 'USA', 'UK'],
'Score': [90, 85, 78, 92, 88, 76]
}
df = pd.DataFrame(data)print("Original DataFrame:")
print(tabulate(df, headers='keys', tablefmt='psql'))Original DataFrame: +----+--------+-------+-----------+---------+ | | Name | Age | Country | Score | |----+--------+-------+-----------+---------| | 0 | John | 28 | USA | 90 | | 1 | Anna | 24 | UK | 85 | | 2 | Peter | 35 | Australia | 78 | | 3 | Linda | 32 | Germany | 92 | | 4 | Phil | 36 | USA | 88 | | 5 | Lucy | 25 | UK | 76 | +----+--------+-------+-----------+---------+
grouped_df = df.groupby('Country').agg({
'Age': 'mean',
'Score': ['sum', 'max']
})print("\nAggregated DataFrame:")
print(tabulate(grouped_df, headers='keys', tablefmt='psql'))Aggregated DataFrame:
+-----------+-------------------+--------------------+--------------------+
| Country | ('Age', 'mean') | ('Score', 'sum') | ('Score', 'max') |
|-----------+-------------------+--------------------+--------------------|
| Australia | 35 | 78 | 78 |
| Germany | 32 | 92 | 92 |
| UK | 24.5 | 161 | 85 |
| USA | 32 | 178 | 90 |
+-----------+-------------------+--------------------+--------------------+