简体   繁体   中英

The pandas group the rows in the dataframe based on the repeated values in the column and repeat all after an Uniqe ID value

*Input DataFrame

 ` C1 C2 0 WID 94556 1 LB INTRO: 2 FNAME Misg, Hugh 3 NAME Misg H 4 AD NY, USA. 5 FNAME Iqbal, Zee 6 NAME Iqbal, Z 7 AD 302, Paris, Fransh. 8 FNAME Talega, hama 9 NAME Talega h 10 AD New York, USA. suresh@usa.us 11 AD Delhi, New Delhi, 12 OTO NOTNLM 13 OTO Nano 14 OT Nano 15 WID 1564576 16 OM 20181211 17 LB INTROD: 18 FNAME Mik, Jeb 19 NAME Mik, J 20 AD SB,UK, 21 FNAME Bala, E 22 NAME Bala E 23 AD Paris, Fransh. 24 LA eng 25 OTO NLM 26 OT agents 27 OT Nano

Now I need to group the dataframe based on the column values(corresponding values separated by ";") and I should get the following (=Number of WID#) different rows.

I tried this df=df['C2'].groupby([df.C1],sort=False).apply('; '.join).reset_index() but it is grouping every unique elements in column and I want in row like bellow ("WID" will be different according to dataset)

Input dataframe

df = pd.DataFrame({'C1' : ['WID', 'LB', 'FNAME', 'NAME', 'AD' ,'FNAME', 'NAME', 'AD','FNAME', 'NAME', 'AD','AD', 'OTO','OTO', 'OT','WID','OM','LB','FNAME','NAME','AD','FNAME','NAME','AD','LA','OTO','OT','OT']
      ,'C2' : ['94556', 'INTRO:','Misg, Hugh'
               ,'Misg H','NY,  USA.',' Iqbal, Zee'
               ,'Iqbal, Z',' 302, Paris, Fransh.','Talega, hama','Talega h','New York, USA.  suresh@usa.us','Delhi, New Delhi,'
               ,'NOTNLM','Nano','Nano','1564576','20181211',' INTROD:',' Mik, Jeb','Mik, J','SB,UK,','Bala, E','  Bala E','Paris, Fransh.','eng','NLM',' agents','  Nano']})

The expected output is:

enter image description here

If it's an image in the link, I think you can achieve it with the following code. If the same columns exist, they need to be pre-combined.

df['new_idx'] = 0
cnt, w = 0, 0 
for i in range(len(df)):
    if df['C1'].iloc[i] == 'WID':
        cnt +=1
    df['new_idx'].iloc[i] = w + cnt

df2 = df.pivot(values='C2', index='new_idx', columns='C1').fillna('')
df2 = df2.iloc[:,[10,6,4,1,5,0,2,9,8]]

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