1
怪異的行爲我有這樣一個數據幀,與大熊貓切,GROUPBY和多指標在Python
Continent % Renewable
Country
China Asia 2
United States North America 1
Japan Asia 1
United Kingdom Europe 1
Russian Federation Europe 2
Canada North America 5
Germany Europe 2
India Asia 1
France Europe 2
South Korea Asia 1
Italy Europe 3
Spain Europe 3
Iran Asia 1
Australia Australia 1
Brazil South America 5
其中% Renewable
是使用切換功能創建了一列,
Top15['% Renewable'] = pd.cut(Top15['% Renewable'], 5, labels=range(1,6))
當我通過Continent
和% Renewable
組來計算每個子集中我所做的國家數量,
count_groups = Top15.groupby(['Continent', '% Renewable']).size()
是,
Continent % Renewable
Asia 1 4
2 1
Australia 1 1
Europe 1 1
2 3
3 2
North America 1 1
5 1
South America 5 1
奇怪的是索引現在,如果我索引值的類別值> 0這給了我的價值,
count_groups.loc['Asia', 1]
>> 4
如果不是,
count_groups.loc['Asia', 3]
>> IndexingError: Too many indexers
它不應該給我一個0,因爲該類別中沒有條目嗎?我會假設,因爲該數據框是使用groupby創建的。
如果沒有,任何人都可以建議一個程序,以便我可以保留012r類別的國家0號?
嘗試'count_groups.get((''Asia',3),0)' – ayhan
我想這將是答案,謝謝 – SamuelNLP