Rollo99
Rollo99

Reputation: 1613

Why storing results of a 'for loop' doesn't work?

I have two dataframes:

daily = pd.DataFrame({'Date': pd.date_range(start="2021-01-01",end="2021-04-29")})
pc21 = pd.DataFrame({'Date': ["21-01-2021", "11-03-2021", "22-04-2021"]})
pc21['Date'] = pd.to_datetime(pc21['Date'])

what I want to do is to create another column for daily with values 1 if dates in pc21 are in daily and 0 otherwise. This is my code:


l=[]

 for i in range(len(pc21['Date'])):
       x = daily['Date'].eq(pc21['Date'][i]).astype(int)
       l.append(x)
       
print(l)

# I also tried:


 for i in range(len(pc21['Date'])):
       daily['newcol'] = daily['Date'].eq(pc21['Date'][i]).astype(int)
       daily['newcol'].append(daily['newcol'])
       

However, I only get saved (for the first code) the last value.

What am I doing wrong?

Can anyone help me?

Thanks!

Upvotes: 0

Views: 90

Answers (3)

Utsav
Utsav

Reputation: 5918

For loop is most probably not working because of scoping issue.

daily['newcol'] = np.where(daily.Date.isin(pc21.Date),1,0)

Details of for loop scoping can be found here:

Scoping in Python 'for' loops

Output Subset

daily.query('newcol.eq(1)')

    Date    newcol
20  2021-01-21  1
111 2021-04-22  1

Upvotes: 1

SeaBean
SeaBean

Reputation: 23207

You can use:

daily['matched'] = daily['Date'].isin(pc21['Date'].to_numpy()).astype(int)

Result checking:

daily[daily['matched'] == 1]



          Date  matched
20  2021-01-21        1
111 2021-04-22        1

Upvotes: 1

Muhammad Safwan
Muhammad Safwan

Reputation: 1024

Don't go for that much complex code just write this one liner code.

daily["Daily"]= daily.Date.isin(pc21.Date).astype(int)

Upvotes: 1

Related Questions