Home > Mobile >  How to use if else type condition in DataFrame without loop?
How to use if else type condition in DataFrame without loop?

Time:02-17

I have a DataFrame df:

enter image description here

I want: if Date1> Date2, then id1 else id2

Output:

enter image description here

How to complete this without using loops? Any hints pls

CodePudding user response:

You can use numpy.where:

import numpy as np

df['output'] = np.where(df['Date1'].gt(df['Date2']), df['Id1'], df['Id2'])

CodePudding user response:

Steps -

  1. Convert the date1 and date2 to datetime object using the pd.to_datetime()
df['date1'] = pd.to_datetime( df.date1)
df['date2'] = pd.to_datetime( df.date2)

  1. Initialize an empty numpy array and then iterate through the rows using df.iterrows() getting proper values into the numpy array and then add a new column.
import numpy as np
n = np.empty(len(df))

for row in df.iterrows():

    # row[1] has the row data
    # row[0] has the index

    if row[1]['date1'] > row[1]['date2']:
        n[row[0]] = 1
    else:
        n[row[0]] = 0
df['output'] = n

There are short hands for this code as well.

  • Related