Home > OS >  Switching from one group of ids to another in 'for' cycle in pandas
Switching from one group of ids to another in 'for' cycle in pandas

Time:03-19

I have dataframe with user data:

d = {'user_id': [9, 9, 9, 9, 9, 9, 26, 26, 26, 26],
            'action_type': [1, 2, 7, 1, 1, 1, 1, 1, 1, 7], 
            'shift_status_id': [1, 1, 1, 1, 1, 1, 1, 1, 4, 1]}
df = pd.DataFrame(data=d)

The goal is to create a function which would count special values on basis of neighboring values for particular user, and than switch to next user. When I implement this:

for i in range(1,len(df)):
    if df['user_id'][i]==df['user_id'][i-1]:
        print("User is the same")
    else:
        print("User has changed!")
        i=i 1

But when I do the same with full dataset (with more columns and rows, but it starts the same) I gen an error:

KeyError: 6 enter image description here What can solve the problem?

CodePudding user response:

I am not sure if this answers your question, but you can also use groupby:

import pandas as pd
d = {'user_id': [9, 9, 9, 9, 9, 9, 26, 26, 26, 26],
            'action_type': [1, 2, 7, 1, 1, 1, 1, 1, 1, 7], 
            'shift_status_id': [1, 1, 1, 1, 1, 1, 1, 1, 4, 1]}
df = pd.DataFrame(data=d)

for group in df.groupby(by='user_id'):
    print(group)
(9,    user_id  action_type  shift_status_id
0        9            1                1
1        9            2                1
2        9            7                1
3        9            1                1
4        9            1                1
5        9            1                1)
(26,    user_id  action_type  shift_status_id
6       26            1                1
7       26            1                1
8       26            1                4
9       26            7                1)

(each group is a tuple, group[0] is the value of the groupby column and group[1] is the remaining dataframe.

  • Related