Count appearances of a value until it changes to another value

前端 未结 6 1127
予麋鹿
予麋鹿 2020-12-17 21:17

I have the following DataFrame:

df = pd.DataFrame([10, 10, 23, 23, 9, 9, 9, 10, 10, 10, 10, 12], columns=[\'values\'])

I want to calculate

6条回答
  •  渐次进展
    2020-12-17 21:34

    This is far from the most time/memory efficient method that in this thread but here's an iterative approach that is pretty straightforward. Please feel encouraged to suggest improvements on this method.

    import pandas as pd
    
    df = pd.DataFrame([10, 10, 23, 23, 9, 9, 9, 10, 10, 10, 10, 12], columns=['values'])
    
    dict_count = {}
    for v in df['values'].unique():
        dict_count[v] = 0
    
    curr_val = df.iloc[0]['values']
    count = 1
    for i in range(1, len(df)):
        if df.iloc[i]['values'] == curr_val:
            count += 1
        else:
            if count > dict_count[curr_val]:
                dict_count[curr_val] = count
            curr_val = df.iloc[i]['values']
            count = 1
    if count > dict_count[curr_val]:
        dict_count[curr_val] = count
    
    df_count = pd.DataFrame(dict_count, index=[0])
    print(df_count)
    

提交回复
热议问题