Home > Mobile >  using apply function write multiple append statement
using apply function write multiple append statement

Time:01-09

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.

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.

CodePudding user response:

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]
  •  Tags:  
  • Related