Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using apply function write multiple append statement

Is it possible to write below code using apply function or any other, because iterrows seems not feasible here.

For creating Dataframe:

df = [[['55792', 'TRIM'], 'A', ['55792', 'TRIM']], [['ZQFC', 'DYWH'], 'A', ['MEIL', '1724']]]
df = pd.DataFrame(df, columns=['seg','cod','seg2'])
df 

seg            cod  seg2
[55792, TRIM]   A   [55792, TRIM]
[ZQFC, DYWH]    A   [MEIL, 1724]


#output
seg            cod  seg2
[55792, TRIM]   A   [55792, TRIM]
[ZQFC, DYWH]    A   [MEIL, 1724]
[MEIL, 1724]    A   [MEIL, 1724]

So, I am expanding the rows if the seg column and seg2 column doesn’t matches. It there any better way to do that using apply.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code I have used:

df1 = pd.DataFrame(columns=df.columns)
for index, row in df.iterrows():
    #if seg match seg 2
    if(row['seg']==row['seg2'])==True:
        #append same row
        df1 = df1.append(row)
    else:
        #if not match, then we can create two seg, and append 2 row
        #append same row first
        df1 = df1.append(row)
        #changing existing segment to new segment, and append
        row['seg'] = row['seg2']
        df1 = df1.append(row)

Thanks a lot.

>Solution :

You don’t need to use apply you can just use your conditionals as boolean masks and do your operations that way.

mask = df["seg"] == df["seg2"]

true_rows = df.loc[mask]
false_rows = df.loc[~mask]
changed_rows = false_rows.assign(seg=false_rows.seg2)

df1 = pd.concat([true_rows, false_rows, changed_rows], ignore_index=True)

print(df1)
             seg cod           seg2
0  [55792, TRIM]   A  [55792, TRIM]
1   [ZQFC, DYWH]   A   [MEIL, 1724]
2   [MEIL, 1724]   A   [MEIL, 1724]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading