Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have the following dataframe.

df = pd.DataFrame(

    {

        "drive": [1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,7,7],

        "team": ['home','home','away','away','away','home','home','home','away',

                 'away','away','home','home','away','away','home','home'],

        "home_comfy_lead": [0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1],

        "home_drives": [1,1,0,0,0,2,2,2,0,0,0,3,3,0,0,4,4],

        'home_drives_with_comfy_lead': [0,0,0,0,0,0,0,1,0,0,0,2,2,0,0,3,3]

    })

I am trying to make two columns:

A home_drives column that uniquely counts the drives from the drive column based on the 'home' designation from the team column.

A home_drives_with_comfy_lead column that uniquely counts the home_drives values based on whether home_comfy_lead is true.

My desired output is:

    drive  team  home_comfy_lead  home_drives  home_drives_with_comfy_lead

0       1  home                0            1                            0

1       1  home                0            1                            0

2       2  away                0            0                            0

3       2  away                0            0                            0

4       2  away                0            0                            0

5       3  home                0            2                            0

6       3  home                0            2                            0

7       3  home                1            2                            1

8       4  away                0            0                            0

9       4  away                0            0                            0

10      4  away                0            0                            0

11      5  home                1            3                            2

12      5  home                1            3                            2

13      6  away                0            0                            0

14      6  away                0            0                            0

15      7  home                1            4                            3

16      7  home                1            4                            3

Can anyone help with this? I've been struggling with this for a few days now.

1 Answer

0 votes
by (41.4k points)

Here,you can use .where to mask and after that groupby + ngroup.

Also,NaN group gets assigned -1 and we want to start counting from 1, so adding +1 will fix both.

df['home_drives'] = df.where(df.team == 'home').groupby('drive').ngroup()+1

df['hdwcl'] = df.where(df.home_comfy_lead == 1).groupby('home_drives').ngroup()+1

Output:

    drive  team  home_comfy_lead  home_drives  hdwcl

0       1  home                0            1      0

1       1  home                0            1      0

2       2  away                0            0      0

3       2  away                0            0      0

4       2  away                0            0      0

5       3  home                0            2      0

6       3  home                0            2      0

7       3  home                1            2      1

8       4  away                0            0      0

9       4  away                0            0      0

10      4  away                0            0      0

11      5  home                1            3      2

12      5  home                1            3      2

13      6  away                0            0      0

14      6  away                0            0      0

15      7  home                1            4      3

16      7  home                1            4      3

If you wish to learn more about how to use python for data science, then go through this data science python course by Intellipaat for more insights.

Browse Categories

...