0

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

1
  • What do you want to do after choosing id1 or id2? You can use df.apply Commented Feb 17, 2022 at 5:03

2 Answers 2

3

You can use numpy.where:

import numpy as np

df['output'] = np.where(df['Date1'].gt(df['Date2']), df['Id1'], df['Id2'])
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

thanks. I am looking for the short hand code
one provided by @Mayank Porwal is good

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.