简体   繁体   English

Python | 熊猫 | CSV | 字典问题

[英]Python | Panda | CSV | Dictionary problem

I have a data which has 40 colums but i narrow it down:我有一个包含 40 列的数据,但我缩小了范围:

Information = df[["Origin Airport", "Operating Airline Name", "Operating Airline   Capacity"]]

Now it looks like this现在看起来像这样

 Origin Airport  ... Operating Airline   Capacity
0                IAD  ...                        16151
1                IAD  ...                        12183
2                IAD  ...                        10974
3                IAD  ...                         8959
4                IAD  ...                         8587
...              ...  ...                          ...
23605            IAD  ...                           50
23606            IAD  ...                           50
23607            IAD  ...                           50
23608            IAD  ...                           50
23609            IAD  ...                           50

I want to make a dictionary with Operating Airline being the Key and capacity as the value.我想制作一本字典,其中 Operating Airline 是 Key,capacity 是 value。

a=Information.set_index('Operating Airline Name')['Operating Airline   Capacity'].to_dict()

It works but when i examine the results it not matching with the actual results.它有效,但是当我检查结果时,它与实际结果不匹配。

Printing "a" looks like this打印“a”看起来像这样

{'Emirates': 16151, 'Ethiopian Airlines Enterprise': 1179, 'Qatar Airways (Q.C.S.C.)': 354, 'Turkish Airlines Inc.': 9300, 'Korean Air Lines Co. Ltd.': 8587, 'United Airlines, Inc.': 126, 'Air France': 296, 'Etihad Airways': 7006, goes on.. {'Emirates': 16151, 'Ethiopian Airlines Enterprise': 1179, 'Qatar Airways (Q.C.S.C.)': 354, 'Turkish Airlines Inc.': 9300, 'Korean Air Lines Co. Ltd.': 8587, 'United Airlines, Inc.': 126, 'Air France': 296, 'Etihad Airways': 7006,继续..

For example there is multiple "Emirates" lines with different values in data but not in "a" dictionary例如,有多个“Emirates”行在数据中有不同的值,但在“a”字典中没有

As a result, I want to have a dictionary that shows the sum of the capacity for each airline.因此,我想要一本字典来显示每家航空公司的运力总和。

Any suggestions?有什么建议么? Thanks!谢谢!

Dictionaries cannot have duplicate keys.字典不能有重复的键。 Your current method is likely taking the first instance of each airline and assigning that capacity to it.您当前的方法可能是采用每家航空公司的第一个实例并将该容量分配给它。 It also has no idea how to aggregate your data.它也不知道如何聚合您的数据。

You need to first create a view of the summarized data before making a dictionary.在制作字典之前,您需要先创建汇总数据的视图。 You can use the .groupby method for this.您可以为此使用.groupby方法。

Try:尝试:

 Information.groupby('Operating Airline Name').agg({'Operating Airline   
 Capacity':'sum'}]).to_dict()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM