我试图找到列值相等的行。或者可能与其他(1个,0.5个等等)或甚至至少2个列相等的差别很小。
df.head(10)
a b c d
0 1128.70 1137.00 1121.30 1132.05
1 1130.20 1142.30 1109.10 1114.90
2 1113.40 1127.90 1109.85 1124.55
3 1126.25 1129.30 1111.20 1124.50
4 1124.45 1141.10 1121.00 1137.95
5 1137.90 1141.90 1094.50 1098.25
6 1097.60 1117.00 1095.65 1112.50
7 1111.05 1119.10 1089.85 1092.10
8 1092.75 1097.60 1074.10 1083.75
9 1083.60 1096.05 1079.10 1087.20
在上表中,我试图找到值相等于彼此(或彼此接近)的行。让我们说:
125 1020.50 1020.50 1020.50 1020.50
452 1047.88 1047.88 1046.95 1048.01
答案 0 :(得分:1)
你能看一下标准偏差吗?
>>> import pandas as pd
>>> pd.DataFrame({'a': {0: 1128.7, 1: 1130.2, 2: 1113.4, 3: 1126.25, 4: 1124.45, 5: 1137.9, 6: 1097.6, 7: 1111.05, 8: 1092.75, 9: 1083.6, 125: 1020.5, 452: 1047.88}, 'b': {0: 1137.0, 1: 1142.3, 2: 1127.9, 3: 1129.3, 4: 1141.1, 5: 1141.9, 6: 1117.0, 7: 1119.1, 8: 1097.6, 9: 1096.05, 125: 1020.5, 452: 1047.88}, 'c': {0: 1121.3, 1: 1109.1, 2: 1109.85, 3: 1111.2, 4: 1121.0, 5: 1094.5, 6: 1095.65, 7: 1089.85, 8: 1074.1, 9: 1079.1, 125: 1020.5, 452: 1046.95}, 'd': {0: 1132.05, 1: 1114.9, 2: 1124.55, 3: 1124.5, 4: 1137.95, 5: 1098.25, 6: 1112.5, 7: 1092.1, 8: 1083.75, 9: 1087.2, 125: 1020.5, 452: 1048.01}})
a b c d
0 1128.70 1137.00 1121.30 1132.05
1 1130.20 1142.30 1109.10 1114.90
2 1113.40 1127.90 1109.85 1124.55
3 1126.25 1129.30 1111.20 1124.50
4 1124.45 1141.10 1121.00 1137.95
5 1137.90 1141.90 1094.50 1098.25
6 1097.60 1117.00 1095.65 1112.50
7 1111.05 1119.10 1089.85 1092.10
8 1092.75 1097.60 1074.10 1083.75
9 1083.60 1096.05 1079.10 1087.20
125 1020.50 1020.50 1020.50 1020.50
452 1047.88 1047.88 1046.95 1048.01
>>> import numpy as np
>>> np.std(df.values, axis=1)
array([ 5.70869676, 13.02005664, 7.50120824, 6.92101645,
8.56084838, 21.84866629, 9.22688836, 12.40707963,
8.97754142, 6.22217556, 0. , 0.42479407])
如果所有值都相等,您可以看到最后两个示例行的标准偏差更低,0
。现在,您只需与阈值进行比较:
>>> n = 1
>>> np.std(df.values, axis=1) < n
array([False, False, False, False, False, False, False, False, False,
False, True, True], dtype=bool)
答案 1 :(得分:0)
你可以在numpy数组中转换你的数据。 npData
然后
rowIndex = [iter for iter in range(npData.shape[0]) if np.std(npData[iter,1:]) <= threshold]
或沿着好轴https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.zscore.html在npData数组上使用scipy中的zscore,然后使用这个zscored数组来查找colomn的每个绝对值之和为&lt; = threshold
我发现至少有两列相同的最佳解决方案:
`from itertools import combinations
n=2
test=[]
for (x1,x2) in combinations(df.values.T,2):
diff = numpy.where(abs(x1-x2)<n)
test = numpy.union1d(test,diff[0])`
或者只是附加测试,然后执行numpy.histogram以找到至少三列或更多列相等的时间