In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('data/src/estat_0003215840.csv')
In [3]:
df = df[['男女別・性比', '人口', '年齢各歳', 'value']]
In [4]:
print(df.head(10))
In [5]:
print(df['男女別・性比'].unique())
In [6]:
print(df['人口'].unique())
In [7]:
print(df[['男女別・性比', '人口']].drop_duplicates())
In [8]:
# df.pivot(index='年齢各歳', columns='男女別・性比', values='value')
# ValueError: Index contains duplicate entries, cannot reshape
In [9]:
df_jp = df.query('人口 == "日本人人口"')
In [10]:
print(df_jp.pivot(index='年齢各歳', columns='男女別・性比', values='value').head(10))
In [11]:
print(df.set_index(['年齢各歳', '人口', '男女別・性比']).unstack(['人口', '男女別・性比']).sort_index(axis=1).head(10))
In [12]:
df_pt = df.pivot_table(index='年齢各歳', columns=['人口', '男女別・性比'], values='value')
In [13]:
print(df_pt.head(10))
In [14]:
print(df_pt.columns)
In [15]:
print(df_pt.loc[:, ('日本人人口', '男')].head(10))
In [16]:
print(df_pt.loc[:, ('日本人人口', ['男', '女'])].head(10))