这应该很简单,但是经过几个小时的搜索,我仍然对自己做错的事情感到茫然。
我尝试了使用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
关于pandas - Pandas 多级索引行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43789309/