我有一个如下表:

             check_churn  is_churn
0               True         1
1               True         1
2              False         1
3              False         1
4               True         1
5               True         1
6               True         1
7               True         1
8               True         1
9               True         1
10              True         1


我想确定多少行是相同的。例如,由于True = 1,所以计算第0行,该行也与第1行相同。因此,答案将是9,因为其中只有2个不匹配。

最佳答案

我相信您需要比较列的值并按True计数sumTrue是类似于1的过程:

print ((df['check_churn'] == df['is_churn']))
0      True
1      True
2     False
3     False
4      True
5      True
6      True
7      True
8      True
9      True
10     True
dtype: bool


print ((df['check_churn'] == df['is_churn']).sum())
9


另一个解决方案是filter并获取DataFrame.shape

print (df_train.loc[df_train.check_churn == df_train.is_churn].shape[0])
9


时间:

np.random.seed(2017)
N = 10000
df = pd.DataFrame({'check_churn':np.random.choice([True, False], size=N),
                   'is_churn':np.random.choice([0, 1], size=N)})
print (df)

In [35]: %timeit (df['check_churn'] == df['is_churn']).sum()
1000 loops, best of 3: 414 µs per loop

In [36]: %timeit sum(df['check_churn'] & df['is_churn'])
1000 loops, best of 3: 793 µs per loop

In [37]: %timeit (df.loc[df.check_churn == df.is_churn].shape[0])
1000 loops, best of 3: 708 µs per loop




N = 1000000

In [39]: %timeit (df['check_churn'] == df['is_churn']).sum()
100 loops, best of 3: 18.2 ms per loop

In [40]: %timeit sum(df['check_churn'] & df['is_churn'])
10 loops, best of 3: 54.7 ms per loop

In [41]: %timeit (df.loc[df.check_churn == df.is_churn].shape[0])
10 loops, best of 3: 23.4 ms per loop

In [42]: %timeit (df['check_churn'] & df['is_churn']).sum()
10 loops, best of 3: 21.2 ms per loop

关于python - 查找两列之间的相同行数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47758720/

10-12 17:00
查看更多