2只熊猫比较并确定阈值

时间:2019-03-05 13:10:00

标签: python-3.x pandas

如何比较2个不同的熊猫csv文件(csv与熊猫),并且由于阈值而返回True或False。

例如 A = [101202405]和B = [103201409],阈值= +-5 B-A = [2 -1 4],因此它将返回True

我该怎么做? 非常感谢。

我的程序就像; a = pd.read_csv(str(myvariable)+'/'+ str(myvariable)+'。csv') b = pd.read_csv(str(counter)+'/'+ str(counter)+'。csv') c =(((b-a).abs()。le(3)),我可以看到ValueError:系列的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。解决方案是什么?

1 个答案:

答案 0 :(得分:1)

差值通过Series.abs获得绝对值后,通过Series.le<=)或Series.lt<)进行比较,并检查所有值是否均为{{ {} {3}}中的1}}秒:

True

详细信息

A = pd.Series([101, 202, 405])
B = pd.Series([103, 201, 409]) 

#less or equal   
C = (B - A).abs().le(5).all()
#less
#C = (B - A).abs().lt(5).all()
print (C)
True

编辑:

对于读取的DataFrame,需要纯python,因为格式不标准(熊猫最好以print ((B - A).abs()) 0 2 1 1 2 4 dtype: int64 print ((B - A).abs().le(5)) 0 True 1 True 2 True dtype: bool 支持读取标量):

read_csv

要比较DataFrame,请使用Series.all

with open('1.csv') as f:
    data1 = f.readlines()

data1 = [[float(y) for y in x.strip().strip('[').strip(']').split()] for x in data1] 
A = pd.DataFrame(data1)
print (A)
       0      1      2
0  166.0  156.0  153.0
1  166.0  156.0  153.0
2  168.0  158.0  154.0

with open('2.csv') as f:
    data2 = f.readlines()

data2 = [[float(y) for y in x.strip().strip('[').strip(']').split()] for x in data2] 
B = pd.DataFrame(data2)
print (B)
       0      1      2
0  142.0  130.0  127.0
1  142.0  130.0  127.0
2  142.0  130.0  126.0