Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a data which has 40 colums but i narrow it down:

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.

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

{'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..

For example there is multiple "Emirates" lines with different values in data but not in "a" dictionary

As a result, I want to have a dictionary that shows the sum of the capacity for each airline.

Any suggestions? Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
309 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

Try:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...