Search code examples
pythonpandasdatedatetimexlsx

Pandas xlsx comparing dates and if equal then give the right col


I have a xlsx like this.

            date   value   ...      
0     2021-07-31  244793   ...        
1     2021-08-01  244685   ...   
2     2021-08-02  453193   ... 
3     2021-08-03  453258   ...  

I want to check and compare the date from today with the date in the xlxs. When the date today and the date in the xlsx is == i want to get the value behind the date. Like today is 2021-07-31 so i get the value 244793.

This is my code.

import pandas
from datetime import datetime

date_today = datetime.now().strftime("%Y-%m-%d")

df = pandas.read_excel('example.xlsx', sheet_name='sheet')

a = df[df['date'] == date_today]

print(a)

I dont know how to complete my idea and i dont find the right tools i need. Maybe someone can help me.


Solution

  • I would think this would work:

    print(a.loc[0,'value'])
    

    But be aware that this only selects the first row (=0) in a. (So if you have multiple rows in the dataframe that match the current date, only the first value of these will be printed, so you would have to loop through the dataframe.)