2017-05-04 171 views
2

這應該是一件簡單的事情,但經過幾個小時的搜索後,我仍然對我做錯的事情感到不知所措。熊貓多級索引行

我試過不同的方法使用MultiIndexing.from_和多個其他的東西,但我不能得到這個權利。

我需要這樣的東西:
enter image description here

而是我得到:
enter image description here 我在做什麼錯?

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) 

回答

3
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 
+0

真棒!非常感謝你。 即使在查看多個示例之後,我錯誤地使用了MultiIndex並使用了錯誤的from_。 – GeorgeLPerkins

+0

@GeorgeLPerkins不用客氣。 – piRSquared

+0

現在我堅持更新特定的值。 我知道如何使用多維Numpy數組,但出於某種原因,我沒有正確解決這些問題。 我可以更新整行,但我無法正確指定列。 – GeorgeLPerkins