3

Hello I added two columns and got a column to display the sum of those two columns successfully. However I want to display other columns along with that summed column. For example I would like to show the ArrDelay and DepDelay or another column if i choose to along with this summed column.

Here is my code below:

flight_data_finalcopy["sumDepArr"] = flight_data_finalcopy["DepDelay"] + flight_data_finalcopy["ArrDelay"]
sum_arrival_delay = flight_data_finalcopy["sumDepArr"]
sum_arrival_delay_fliter = sum_arrival_delay[["UniqueCarrier", "AirlineID", "DepDelay", "ArrDelay", "sumDepArr"]]

print(display(sum_arrival_delay_filter))    

2 Answers 2

3

You can append the new column to your dataframe:

flight_data_finalcopy["sumDepArr"] = flight_data_finalcopy["DepDelay"] + flight_data_finalcopy["ArrDelay"]

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

thanks ! butsorry maybe i wasnt clear i updated my code above, I want to try display those columns along with the column "sumDepArr" @tomasn4a
1

I think you need assign it to new column:

flight_data_finalcopy['new'] = flight_data_finalcopy["DepDelay"] + 
                               flight_data_finalcopy["ArrDelay"]

Or use add:

flight_data_finalcopy['new'] = flight_data_finalcopy["DepDelay"].add(
                               flight_data_finalcopy["ArrDelay"])

EDIT by comment:

Remove sum_arrival_delay = flight_data_finalcopy["sumDepArr"] and then replace sum_arrival_delay to flight_data_finalcopy:

Sample:

flight_data_finalcopy = pd.DataFrame({'DepDelay':[1,2,3],
                   'ArrDelay':[4,5,6],
                   'UniqueCarrier':[7,8,9],
                   'AirlineID':[1,3,5],
                   'Another_column':[4,8,9]})

print (flight_data_finalcopy)
   AirlineID  Another_column  ArrDelay  DepDelay  UniqueCarrier
0          1               4         4         1              7
1          3               8         5         2              8
2          5               9         6         3              9


flight_data_finalcopy["sumDepArr"] = flight_data_finalcopy["DepDelay"] + flight_data_finalcopy["ArrDelay"]
sum_arrival_delay_filter = flight_data_finalcopy[["UniqueCarrier", "AirlineID", "DepDelay", "ArrDelay", "sumDepArr"]]
print (sum_arrival_delay_filter)   
   UniqueCarrier  AirlineID  DepDelay  ArrDelay  sumDepArr
0              7          1         1         4          5
1              8          3         2         5          7
2              9          5         3         6          9

2 Comments

maybe i was not clear enough earlier on what i wanted. I updated the code above! Please at it again thanks @jezrael
I looked over and saw what i missed out , Thanks worked like a charm! @jezrael

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.