Alessandrini
Alessandrini

Reputation: 191

Create new row in Pandas DataFrame based on conditions

I want to iterate over every row in the dataframe and create another row if it matches certain criteria. For example

       A                 B           C
       Both             999          LP
       London           1002         KI
       Manchester       1005         BV
       Both             1001         IG

Will result in:

       A                 B                   C
       Both             999                  LP
       Both             (Some calculation)   LP
       London           1002                 KI
       Manchester       1005                 BV
       Both             1001                 IG

We look at every row and if column A equals "Both" and B is less than 1000, insert a new row where col A stays the same, col B is based on a function I have written and col C stays the same.

def create_new_row(row):
    if row['A'] == "Both":
        if row['B'] < 1000:
             result = somefuction(row['B'])

    return result

This is the code I have so far but I can't work out how to get the dataframe I want.

Upvotes: 1

Views: 1689

Answers (1)

jezrael
jezrael

Reputation: 862581

Filter the dataframe to get the rows that satisfy your condition by boolean indexing:

m = (df['A'] == "Both") & (df['B'] < 1000)
df1 = df[m].copy()

Generate the new lines based on the filtered database:

#sample function
def somefuction(x):
    return x / 100

df1['B'] = df1['B'].apply(somefuction)

Join the result with the original dataframe by concat and sorting for correct order by DataFrame.sort_index, last create default index by reset_index:

df = pd.concat([df, df1]).sort_index(kind='mergesort').reset_index(drop=True)
print (df)
            A        B   C
0        Both   999.00  LP
1        Both     9.99  LP
2      London  1002.00  KI
3  Manchester  1005.00  BV
4        Both  1001.00  IG

Upvotes: 3

Related Questions