Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading