Home > Mobile >  Elementwise comparison of lists of different lengths
Elementwise comparison of lists of different lengths

Time:01-14

I have the following DataFrame:

df=

   datetime            col1 col2
0  2023-01-01 12:00:00 100  200
1  2023-01-02 12:00:00 120  400
2  2023-01-03 12:00:00 140  500
3  2023-01-04 12:00:00 160  700
4  2023-01-05 12:00:00 200  300
5  2023-01-06 12:00:00 430  200
6  2023-01-07 12:00:00 890  100

And I have the list below:

dates = [2023-01-01, 2023-01-03, 2023-01-07]

I want to create a new column and fill it with ones for the items of the list that are equal with the date part of df['datetime'].

With the code below I am able to compare the elements of the list with the ones in df['datetime'].

import numpy as np
np.isin(dates, pd.DatetimeIndex(df['datetime']).date)

I have tried to implement the following code but it is not working due to different sizes:

np.where(np.isin(dates, pd.DatetimeIndex(df['datetime']).date),df['col3']==1,df['col3']==0)

The output should look like this:

df=

   datetime            col1 col2 col3
0  2023-01-01 12:00:00 100  200  1
1  2023-01-02 12:00:00 120  400  0
2  2023-01-03 12:00:00 140  500  1
3  2023-01-04 12:00:00 160  700  0
4  2023-01-05 12:00:00 200  300  0
5  2023-01-06 12:00:00 430  200  0
6  2023-01-07 12:00:00 890  100  1

CodePudding user response:

You can use a lambda function:

df['col3'] = df['datetime'].apply(lambda x: 1 if x in dates else 0)

and get

   datetime            col1 col2 col3
0  2023-01-01 12:00:00 100  200  1
1  2023-01-02 12:00:00 120  400  0
2  2023-01-03 12:00:00 140  500  1
3  2023-01-04 12:00:00 160  700  0
4  2023-01-05 12:00:00 200  300  0
5  2023-01-06 12:00:00 430  200  0
6  2023-01-07 12:00:00 890  100  1
  • Related