import os
import os.path
import pandas as pd
datadir = "publicdata"
ind_pivot_LoL = [['CAN', 'pop', 36.26],
['CAN', 'gdp', 1535.77],
['CAN', 'life', 82.30],
['USA', 'pop', 323.13],
['USA', 'gdp', 18624.47],
['USA', 'life', 76.25]]
dataset_columns = ['code', 'ind', 'value']
ind_to_pivot = pd.DataFrame(ind_pivot_LoL, columns=dataset_columns)
# Pivot with index of code, and pivot column of ind
row_labels = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2']
column_labels = ['I', 'J', 'K', 'V']
dataset = [[ 'A.I', 'A.J', 'K.1', 'A1.V'],
[ 'A.I', 'A.J', 'K.2', 'A2.V'],
[ 'B.I', 'B.J', 'K.1', 'B1.V'],
[ 'B.I', 'B.J', 'K.2', 'B2.V'],
[ 'C.I', 'C.J', 'K.1', 'C1.V'],
[ 'C.I', 'C.J', 'K.2', 'C2.V'],
]
pivot_ex2 = pd.DataFrame(dataset, columns=column_labels, index=row_labels)
pivot_ex1 = pivot_ex2.drop('J', axis=1)
pivot_ex1
# Pivot with index of I and pivot column of K (with explicit value column of V)
row_labels = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2']
column_labels = ['I', 'K', 'V1', 'V2']
dataset = [[ 'A.I', 'K.1', 'A1.V1', 'A1.V2'],
[ 'A.I', 'K.2', 'A2.V1', 'A2.V2'],
[ 'B.I', 'K.1', 'B1.V1', 'B1.V2'],
[ 'B.I', 'K.2', 'B2.V1', 'B2.V2'],
[ 'C.I', 'K.1', 'C1.V1', 'C1.V2'],
[ 'C.I', 'K.2', 'C2.V1', 'C2.V2']
]
pivot_ex2 = pd.DataFrame(dataset, columns=column_labels, index=row_labels)
# Pivot with index of I, pivot column of K, and *two* value columns of V1 and V2
melted_names = pd.read_csv(os.path.join(datadir, "melted_names.csv"))
melted_names
# Pivot with index of sex, pivot column of year, and value of name
# Pivot with index of year, pivot column of sex, and value of name
indicators = pd.read_csv(os.path.join(datadir, "indicators_ex.csv"))
indicators.head()
# Pivot with index of code, and pivot column of year
# Pivot with index of year, and pivot column of code
datarows = [
['CHN', 2010, 1337.70, 6087.16],
['CHN', 2015, 1371.22, 11015.54],
['CHN', 2018, 1392.73, 13608.15],
['GBR', 2010, 62.77, 2452.90],
['GBR', 2015, 65.13, 2896.42],
['IND', 2010, 1234.28, 1675.62],
['IND', 2015, 1310.15, 2103.59]]
indicators = pd.DataFrame(datarows, columns=['code','year','pop','gdp'])
# Pivot with index of code and pivot column of year
column_labels = ['I', 'P', 'V1', 'V2']
dataset = [['A', 'w', 15, 23.5],
['A', 'x', 10, 42.5],
['A', 'x', 5, 18.0],
['B', 'w', 8, 10.2],
['B', 'w', 4, 14.3],
['B', 'x', 6, 12.5]]
table = pd.DataFrame(dataset, columns=column_labels)
# Attempt at pivot with index of I and pivot column of P
# pivot table with index of I and pivot column of P
pivoted = table.pivot_table(index='I', columns='P',
aggfunc={'V1': 'mean', 'V2': 'sum'})