Files
training.python.datascience/source/pandas/01-pandas-actions/01-dataframe-pivot.py

13 lines
544 B
Python

import pandas as pd
if __name__ == '__main__':
df = pd.DataFrame({
'country': ['fra', 'fra', 'fra', 'fra', 'bel', 'bel', 'bel', "bel"],
'district': ['north', 'east', 'west', 'south', 'north', 'east', 'west', 'south'],
'population': [10_000, 30_000, 50_000, 70_000, 20_000, 40_000, 60_000, 80_000],
'extra': ['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o']
})
df2 = df.pivot_table(values=["population"], index="district", columns="country", aggfunc="count")
print(df2.to_string())
print(df2.columns)