2016-09-16 42 views
1

我想從我的數據框中創建字典。數據幀是這樣的:在Python中從數據框創建字典

Vehical | Brand | Model Name 
car  | Suzuki | abc 
car  | Honda | def 
bike | Suzuki | xyz 
bike | Honda | asd 

我想我的字典,如:

{car : {Suzuki : abc, Honda : def}, bike : {Suzuki : xyz, Honda : asd}} 

回答

2

你能做到這樣:

In [28]: df.pivot(index='Vehical', columns='Brand', values='Model Name') 
Out[28]: 
Brand Honda Suzuki 
Vehical 
bike  asd xyz 
car  def abc 
+0

:旋轉的

In [29]: df.pivot(index='Vehical', columns='Brand', values='Model Name').to_dict('i') Out[29]: {'bike': {'Honda': 'asd', 'Suzuki': 'xyz'}, 'car': {'Honda': 'def', 'Suzuki': 'abc'}} 

結果謝謝MaxU .....它工作 – Tarun