我有一个类似的数据框:
ID value
111 1
111 0
111 1
111 0
111 0
111 0
111 1
222 1
222 0
222 0
222 1
对于每个ID,我需要连续出现0
次的最大次数。
在这种情况下,由于0
连续三次显示ID 111
,连续两次显示222
,因此所需的输出应为:
ID count_max_0
111 3
222 2
value_counts
没有做我想要的事情,因为它计算了列中的所有值。
我该怎么做?
答案 0 :(得分:2)
您可以使用
iszero = (df['value']==0)
df['group'] = (iszero.diff()==1).cumsum()
分配每行的组号:
In [115]: df
Out[115]:
ID value group
0 111 1 0
1 111 0 1
2 111 1 2
3 111 0 3
4 111 0 3
5 111 0 3
6 111 1 4
7 222 1 4
8 222 0 5
9 222 0 5
10 222 1 6
现在,您可以按ID
和group
分组,以获得所需的值计数:
import pandas as pd
df = pd.DataFrame({'ID': [111, 111, 111, 111, 111, 111, 111, 222, 222, 222, 222],
'value': [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1]})
iszero = (df['value']==0)
df['group'] = (iszero.diff()==1).cumsum()
counts = (df.loc[iszero] # restrict to rows which have 0 value
.groupby('ID')['group'] # group by ID, inspect the group column
.value_counts() # count the number of 0s for each (ID, group)
.groupby(level='ID') # group by ID only
.first()) # select the first (and highest) value count
print(counts)
产量
ID
111 3
222 2
Name: group, dtype: int64
答案 1 :(得分:0)
这应该有效:
import numpy as np
# load data etc
...
def get_count_max_0(df):
"""
Computes the max length of a sequence of zeroes
broken by ones.
"""
values = np.array(df['value'].tolist())
# compute change points where 0 -> 1
cps_1 = np.where(
(values[1:] != values[:-1]) &
(values[1:] == 1)
)[0]
# compute change points where 1 -> 0
cps_0 = np.where(
(values[1:] != values[:-1]) &
(values[1:] == 0)
)[0]
# find lengths of zero chains
deltas = cps_1 - cps_0
# get index of max length
idx = np.where(deltas == deltas.max())[0][0]
# return max length
return deltas[idx]
# group by ID, apply get_count_max_0 to each group and
# convert resulting series back to data frame to match your expected output.
max_counts = df.groupby("ID").apply(get_count_max_0).to_frame("count_max_0")
print(max_counts)
输出结果为:
count_max_0
ID
111 3
222 2
答案 2 :(得分:0)
aggregations = {
'value': {
'total': 'sum'
}
}
dftwo = df.groupby('ID').agg(aggregations)