Rock
Rock

Reputation: 2967

Find value of one column within another column

I have a dataframe:

df = pd.DataFrame({'start': ['2020-08-01', '2020-08-02', '2020-08-03', '2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07', '2020-08-08'],
                   'end': ['2020-08-03', '', '', '2020-08-06', '', '2020-08-08', '', ''],
                   'score': [74, 81, 38, 49, 79, 17, 53, 69]})

df

         start         end  score
0   2020-08-01  2020-08-03     74
1   2020-08-02                 81
2   2020-08-03                 38
3   2020-08-04  2020-08-06     49
4   2020-08-05                 79
5   2020-08-06  2020-08-08     17
6   2020-08-07                 53
7   2020-08-08                 69

that I need to add a new column in which the row is marked as 1 when end date is found in start, that is:

         start         end  score  mark
0   2020-08-01  2020-08-03     74
1   2020-08-02                 81
2   2020-08-03                 38     1
3   2020-08-04  2020-08-06     49
4   2020-08-05                 79
5   2020-08-06  2020-08-08     17     1
6   2020-08-07                 53
7   2020-08-08                 69     1

What's a good pandas solution to do this? Many thanks!

Upvotes: 0

Views: 86

Answers (2)

wwnde
wwnde

Reputation: 26676

df['mark']=np.where(df.start.isin(df.end),'1',' ')

Upvotes: 1

BENY
BENY

Reputation: 323226

Check with

s = df['start'].isin(df['end']).astype(int)
df['New'] = s.where(s==1 ,'')

Upvotes: 2

Related Questions