简体   繁体   中英

How can combine multiple rows into single based on condition in pandas

Hi I want to combine multiple rows into single based on status success/failed. Please help me how can I do this?

Example-

Region    Sub_Region        Status    Reason
ASPAC     CRM               success
ASPAC     Genesys           success
ASPAC     Survey            success   
ASPAC     Survey_Response   failed    list index out of range
ASPAC     Survey_Int        success
LATAM     CRM               success
LATAM     Survey            success
LATAM     Survey_Response   success
LATAM     Survey_Int        success

If all 3 - Survey, Survey_Response & Survey_Int are success then only combine them into 1 as Survey only. If any of 3 is failed then Status will be failed and in reason particular sub_reson should come. Please follow the below example-

Region    Sub_Region        Status    Reason
ASPAC     CRM               success
ASPAC     Genesys           success
ASPAC     Survey            failed    Survey_Response Failed - list index out of range
LATAM     CRM               success
LATAM     Survey            success
    

Use:

#filter failed rows and aggregate join
m = df['Status'].eq('failed')

#joined columns only if failed
df['Reason'] = (df['Sub_Region'] + ' Failed - ' + df['Reason']).where(m , '')
#remove substring afer first _
df['Sub_Region'] = df['Sub_Region'].str.split('_').str[0]


df1 = df[m].groupby(['Region','Sub_Region','Status'])['Reason'].agg(','.join).reset_index()
#append no failed rows and remove duplicates
df2 = (pd.concat([df1, df[~m]])
         .drop_duplicates(['Region','Sub_Region'])
         .sort_values(['Region','Sub_Region'], ignore_index=True))

print (df2)
  Region Sub_Region   Status                                    Reason
0  ASPAC        CRM  success                                          
1  ASPAC    Genesys  success                                          
2  ASPAC     Survey   failed  Survey_Response Failed - list index out of range
3  LATAM        CRM  success                                          
4  LATAM     Survey  success                                          

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