简体   繁体   中英

Subtracting values from two data frames given same value in column

I currently have two data frames with the same number of columns and column names. Two of the columns in both data frames are 'Time' and "Trip_ID'. In this case I want to subtract the Time values between both dataframes if the rows have the same Trip_ID.

Example -

Time   Trip_ID
15     X
20     Y
25     Z

Time   Trip_ID
35     Z
40     Y
65     X

I want to have a list of all of the differences in time which, in this case, would look like (50, 20, 10)

In one line:

(df2.set_index('Trip_ID')- df.set_index('Trip_ID')).squeeze().tolist()

Output:

Out[14]: [50, 20, 10]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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