Pandas 行的多级索引
Pandas Multilevel index for rows
这应该是一件简单的事情,但是经过几个小时的搜索,我仍然不知道自己做错了什么。
我尝试了使用 MultiIndexing.from_ 和其他多种方法的不同方法,但我就是做不对。
我需要这样的东西:
但是我得到:
我做错了什么?
import pandas as pd
list_of_customers = ['Client1', 'Client2', 'Client3']
stat_index = ['max', 'current', 'min']
list_of_historic_timeframes = ['16:10', '16:20', '16:30']
timeblock = pd.DataFrame(index=([list_of_customers, stat_index]), columns=list_of_historic_timeframes)
timeblock.fillna(0, inplace=True)
print(timeblock)
list_of_customers = ['Client1', 'Client2', 'Client3']
stat_index = ['max', 'current', 'min']
list_of_historic_timeframes = ['16:10', '16:20', '16:30']
timeblock = pd.DataFrame(
0,
pd.MultiIndex.from_product(
[list_of_customers, stat_index],
names=['Customer', 'Stat']
),
list_of_historic_timeframes
)
print(timeblock)
16:10 16:20 16:30
Customer Stat
Client1 max 0 0 0
current 0 0 0
min 0 0 0
Client2 max 0 0 0
current 0 0 0
min 0 0 0
Client3 max 0 0 0
current 0 0 0
min 0 0 0
这应该是一件简单的事情,但是经过几个小时的搜索,我仍然不知道自己做错了什么。
我尝试了使用 MultiIndexing.from_ 和其他多种方法的不同方法,但我就是做不对。
我需要这样的东西:
但是我得到:
import pandas as pd
list_of_customers = ['Client1', 'Client2', 'Client3']
stat_index = ['max', 'current', 'min']
list_of_historic_timeframes = ['16:10', '16:20', '16:30']
timeblock = pd.DataFrame(index=([list_of_customers, stat_index]), columns=list_of_historic_timeframes)
timeblock.fillna(0, inplace=True)
print(timeblock)
list_of_customers = ['Client1', 'Client2', 'Client3']
stat_index = ['max', 'current', 'min']
list_of_historic_timeframes = ['16:10', '16:20', '16:30']
timeblock = pd.DataFrame(
0,
pd.MultiIndex.from_product(
[list_of_customers, stat_index],
names=['Customer', 'Stat']
),
list_of_historic_timeframes
)
print(timeblock)
16:10 16:20 16:30
Customer Stat
Client1 max 0 0 0
current 0 0 0
min 0 0 0
Client2 max 0 0 0
current 0 0 0
min 0 0 0
Client3 max 0 0 0
current 0 0 0
min 0 0 0