[英]Find the time difference in two dataframes
df1:
time_1 a b c
0 1.675168e+09 -90.56 5.28 -6.23
1 1.675168e+09 -87.98 5.27 -5.68
2 1.675168e+09 -83.96 14.74 -9.44
3 1.675168e+09 -85.58 -5.72 -5.27
4 1.675168e+09 -95.13 -4.15 -5.46
5 1.675168e+09 -90.56 5.28 -6.23
6 1.675168e+09 -87.98 5.27 -5.68
7 1.675168e+09 -83.96 14.74 -9.44
8 1.675168e+09 -85.58 -5.72 -5.27
9 1.675168e+09 -95.13 -4.15 -5.46
df2:
time_2 x y z
0 1.675168e+09 -6.64 542.397494 2.25
1 1.675168e+09 -6.64 541.233179 2.25
2 1.675169e+09 -6.63 567.644365 2.25
3 1.675169e+09 -6.63 530.368776 2.25
4 1.675170e+09 -6.63 552.896863 2.25
我想得到时差。 即,df1中的time_1减去df2中的所有time_2值。
df:
time_1 - time_2 a b c y
0 1.675168e+09 - 1.675168e+09
1 1.675168e+09 - 1.675168e+09
2 1.675168e+09 - 1.675169e+09
3 1.675168e+09 - 1.675169e+09
4 1.675168e+09 - 1.675170e+09
5
6
7
和 go 上
根据您的评论更新,使用merge
with how='cross'
:
out = df1.merge(df2, how='cross').assign(time=lambda x: x.pop('time_1') - x.pop('time_2'))
print(out)
# Output
a b c x y z time
0 -90.56 5.28 -6.23 -6.64 542.397494 2.25 0.0
1 -90.56 5.28 -6.23 -6.64 541.233179 2.25 0.0
2 -90.56 5.28 -6.23 -6.63 567.644365 2.25 -1000.0
3 -90.56 5.28 -6.23 -6.63 530.368776 2.25 -1000.0
4 -90.56 5.28 -6.23 -6.63 552.896863 2.25 -2000.0
...
45 -95.13 -4.15 -5.46 -6.64 542.397494 2.25 0.0
46 -95.13 -4.15 -5.46 -6.64 541.233179 2.25 0.0
47 -95.13 -4.15 -5.46 -6.63 567.644365 2.25 -1000.0
48 -95.13 -4.15 -5.46 -6.63 530.368776 2.25 -1000.0
49 -95.13 -4.15 -5.46 -6.63 552.896863 2.25 -2000.0
您可以加入您的数据框(基于索引):
out = df2.join(df1).assign(time=lambda x: x.pop('time_1') - x.pop('time_2'))
print(out)
# Output
x y z a b c time
0 -6.64 542.397494 2.25 -90.56 5.28 -6.23 0.0
1 -6.64 541.233179 2.25 -87.98 5.27 -5.68 0.0
2 -6.63 567.644365 2.25 -83.96 14.74 -9.44 -1000.0
3 -6.63 530.368776 2.25 -85.58 -5.72 -5.27 -1000.0
4 -6.63 552.896863 2.25 -95.13 -4.15 -5.46 -2000.0
df = pd.DataFrame({'time_1': [1, -1, 1.5]},
)
df1 = pd.DataFrame({'time_2': [-2.5, 1.5, 2.6]},
)
#combine the two dataframes into one, concat on column axis
df2 = pd.concat([df, df1],axis="columns")
#assign new column with difference between time
df2 = df2.assign(Time_diff = df2['time_1'] - df2['time_2'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.