gpt4 book ai didi

python - 我如何获得 Pandas 中一行的相等或最接近的值?

转载 作者:行者123 更新时间:2023-11-28 18:13:10 25 4
gpt4 key购买 nike

我试图找到列值彼此相等的行。或者可能与其他(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

最佳答案

能不能只看标准差?

>>> 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)

关于python - 我如何获得 Pandas 中一行的相等或最接近的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50016973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com