简体   繁体   English

从列中给定相同值的两个数据框中减去值

[英]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.两个数据帧中的两列是“时间”和“Trip_ID”。在这种情况下,如果行具有相同的 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)我想列出所有时间差异,在这种情况下,它看起来像 (50, 20, 10)

In one line:在一行中:

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

Output:输出:

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

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

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