繁体   English   中英

比较 2 个熊猫数据帧并输出不等值的 Python

[英]Comparing 2 pandas dataframes and outputting inequivalent values Python

我想要写在一块代码pandas那里得到2个的数据帧Unix,Unix2对它们进行比较,并输出其中存在差异的索引的范围内。 例如指数1具有1444311780用于Unix1444311790用于Unix2的值UnixUnix2是不同的,因此它将使指数1为起始范围。 结束范围将是不等式的最后一个连续值,因此索引 2 将16356860401635686034Unix, Unix2进行比较。

import time
import datetime
import pandas as pd 

Unix= pd.DataFrame([1444311600, 1444311780, 1635686040, 1635686200, 1635686220])
Unix2 = pd.DataFrame([1444311600, 1444311790, 1635686034, 1635686200, 1635686230])

预期输出:

first       last        
1           2  
4              

如果我理解正确,您想找到每个不等连胜的开始和结束索引。 尝试这个:

# Compare Unix to Unix2, row-by-row
s = Unix[0] != Unix2[0]

# Assign the group number. Every time `s` flips from True to False
# or vice-versa, make a new group
t = s.ne(s.shift()).cumsum()

# Filter for the groups whose members are all True
u = t[s]

# For those groups, find the min and the max index of their members
result = u.index.to_series().groupby(u).agg(['min', 'max'])

输出:

   min  max
0          
2    1    2
4    4    4

暂无
暂无

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

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