0

I have a pandas df as follows:

Name    Cust1   Cust2   Cust3      Cust4

ABC      Y       N       Y       2022-01-01
DEF      N       N       N          N

I am looking to detect if a date is in a row for Cust1, Cust2, Cust3 and Cust4 and if so create a column to populate that date. So that output would look like

Name    Date

ABC    2022-01-01
DEF      na

Any ideas on how I can do this? I am trying to do df.iloc[:,1:].apply(np.where<xxx> but not sure of how to approach this from here on.

Thanks!

4
  • If possible multiple dates per rows what is expected ouput? Commented Mar 16, 2022 at 6:31
  • 1
    Good catch, in that case I want to take the latest date (as in most recent date). Thanks! Commented Mar 16, 2022 at 6:32
  • Performance is important? Commented Mar 16, 2022 at 7:32
  • 1
    Not really...performance is secondary here..not many rows of data. Thanks! Commented Mar 16, 2022 at 16:27

2 Answers 2

2

You can flatten your dataframe then keep the most recent date per Name:

to_date = lambda x: pd.to_datetime(x['Date'], errors='coerce')
out = df.melt('Name', value_name='Date').assign(Date=to_date) \
        .groupby('Name', as_index=False)['Date'].max()
print(out)

# Output
  Name       Date
0  ABC 2022-01-01
1  DEF        NaT
Sign up to request clarification or add additional context in comments.

Comments

1

Try convert values of all columns to datetimes by to_datetime with errors='coerce' for missing values if not datetimelike values and then get maximal dates per rows:

f = lambda x: pd.to_datetime(x, errors='coerce')
df = df.set_index('Name').apply(f).max(axis=1).reset_index(name='Date')
print (df)
  Name       Date
0  ABC 2022-01-01
1  DEF        NaT

Alternative solution:

f = lambda x: pd.to_datetime(x, errors='coerce')
df = df[['Name']].join(df.iloc[:,1:].apply(f).max(axis=1).rename('Date'))
print (df)
  Name       Date
0  ABC 2022-01-01
1  DEF        NaT

Comments

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.