如何在列的起始行和结束行之间找到单元格值的重复记录计数?

2024-06-12 02:39:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把excel公式=COUNTIF(A4:A11,A4)转换成python代码。 这里A4是我选择的开始行,A11是结束行,我想找出从A4行到A11的单元格中A4的值出现的次数。你知道吗

我已将excel文件中的数据加载到数据框中。你知道吗


Tags: 文件数据代码次数excel公式a4a11
3条回答

下面是一个数据帧示例。你知道吗

In [307]: df
Out[307]:
    a
0   0
1   1
2   2
3   3
4   1
5   1
6   4
7   5
8   1
9   1
10  5
11  0
12  0
13  0
14  1

In [308]: df[df['a'] == df['a'].iloc[4]]['a'].loc[4:11].count()
Out[308]: 4

或者

In [315]: df[df['a'] == df['a'].iloc[4]].loc[4:11, 'a'].count()
Out[315]: 4

或者

In [323]: df.loc[4:11].query('a== @df.a.iloc[4]')['a'].count()
Out[323]: 4

或者

In [319]: df.loc[4:11, 'a'].eq(df.a.iloc[4]).sum()
Out[319]: 4

使用numpy

np.count_nonzero(np.in1d(df['a'][4:12],df['a'][4]))

让我们从填充数据帧开始(您可能正在使用read\u csv或其他东西)。你知道吗

df = pd.DataFrame({"a": [1, 0, 2, 3, 1, 1, 3, 5,1, 0, 1, 3, 7, 9]})

您可以使用

df[4:11+1] # does the same thing as `A4:A11` in terms of grabbing only those rows

从这里开始,我们要限制为只匹配A4的值。我们这样做是因为

df[4:11+1][df["a"] == df["a"][4]]

然后,我们要计算这些事件:

df[4:11+1][df["a"] == df["a"][4]]["a"].count()

相关问题 更多 >