简体   繁体   中英

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!

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM