Pandas group-by conditional filtering

ccsv

I have a DataFrame:

import pandas as pd

df = pd.DataFrame({'First': ['Sam', 'Greg', 'Steve', 'Sam',
                             'Jill', 'Bill', 'Nod', 'Mallory', 'Ping', 'Lamar'],
                   'Last': ['Stevens', 'Hamcunning', 'Strange', 'Stevens',
                            'Vargas', 'Simon', 'Purple', 'Green', 'Simon', 'Simon'],
                   'Address': ['112 Fake St',
                               '13 Crest St',
                               '14 Main St',
                               '112 Fake St',
                               '2 Morningwood',
                               '7 Cotton Dr',
                               '14 Main St',
                               '20 Main St',
                               '7 Cotton Dr',
                               '7 Cotton Dr'],
                   'Status': ['Infected', '', 'Infected', '', '', '', '','', '', 'Infected'],
                   })

And I apply the following group-by code

df_index = df.groupby(['Address', 'Last']).filter(lambda x: (x['Status'] == 'Infected').any()).index
df.loc[df_index, 'Status'] = 'Infected'

Instead of marking everything as "Infected" as in the group-by code. Is there a method to select the values that will be updated so they can be marked as something else? For example:

df2 = df.copy(deep=True)
df2['Status'] = ['Infected', '', 'Infected', 'Infected2', '', 'Infected2', '', '', 'Infected2', 'Infected']
Marius

I think this achieves your desired result, going at it slightly differently:

def infect_new_people(group):
    if (group['Status'] == 'Infected').any():
        # Only affect people not already infected
        group.loc[group['Status'] != 'Infected', 'Status'] = 'Infected2'
    return group['Status']

# Need group_keys=False so that each group has the same index
#   as the original dataframe
df['Status'] = df.groupby(['Address', 'Last'], group_keys=False).apply(infect_new_people)

df
Out[36]: 
         Address    First        Last     Status
0    112 Fake St      Sam     Stevens   Infected
1    13 Crest St     Greg  Hamcunning           
2     14 Main St    Steve     Strange   Infected
3    112 Fake St      Sam     Stevens  Infected2
4  2 Morningwood     Jill      Vargas           
5    7 Cotton Dr     Bill       Simon  Infected2
6     14 Main St      Nod      Purple           
7     20 Main St  Mallory       Green           
8    7 Cotton Dr     Ping       Simon  Infected2
9    7 Cotton Dr    Lamar       Simon   Infected

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related