简体   繁体   中英

Comparing 2 duplicated values in a column in Pandas

I have a column with the customers name which is duplicated when the customers have 2 products. I have to create a new status to group the customers status in ONE depending on the situation. So I have to compare the customer X to the another X to generate a new status as one.

Customer|Status  |Cancaled_at|new status
X       |Active  |-          |
X       |Canceled|2019-xx-xx |
Y       |Active  |-          |
Z       |Active  |-          |
A       |Canceled|-          |

desired output:

Customer|Status  |Cancaled_at|new status
X       |Active  |-          |Canceled
X       |Canceled|2019-xx-xx |Canceled
Y       |Active  |-          |
Z       |Active  |-          |
A       |Canceled|-          |

There is an easy way to find all duplicated values in pandas:

df['new_status'][(df.duplicated('Customer', False))] = 'Canceled'

This makes new_status column Canceled where dataframe's Customer column has duplicated values.

I think you need:

df = pd.DataFrame({'Customer':['X','X','Y','Z','A'], 'status':['active','canceled','active','active','canceled'],
    'Canceled_at':[None, '2019-01-01', None, None,None]})


df['new_status'] = np.where((df['status']=='canceled') & (~df['Canceled_at'].isnull()), 'canceled', None)
df['new_status'] = df.groupby('Customer')['new_status'].bfill()

print(df)

Output:

 Canceled_at   Customer    status new_status                                                                                           
0        None        X    active   canceled                                                                                           
1  2019-01-01        X  canceled   canceled                                                                                           
2        None        Y    active       None                                                                                           
3        None        Z    active       None                                                                                           
4        None        A  canceled       None

This code uses the sort_values() , fillna() and shift() :

df = df.sort_values(by=['Customer', 'Status'])
df['new_status'] = df[df.Status == 'Canceled']['Status']
df.loc[((df['Customer'] != df['Customer'].shift(-1)) & (df['new_status'].isnull())), 'new_status'] = ''
df['new_status'].fillna(method = 'backfill', limit = 1, inplace = True)
df.sort_index(inplace = True)

Produces the following output:

    Customer    Status      Cancaled_at new_status
0   X           Active      -           Canceled
1   X           Canceled    2019-xx-xx  Canceled
2   Y           Active      -   
3   Z           Active      -   
4   A           Canceled    -           Canceled

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM