![]() |
Trying to Pass date to pandas search from input prompt - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Trying to Pass date to pandas search from input prompt (/thread-21683.html) |
Trying to Pass date to pandas search from input prompt - curranjohn46 - Oct-09-2019 Hi, I am trying to figure out how to pass a date inputted at a prompt by the user to pandas to search by date. I have both the search and the input prompt working separately but not together. I will show you what I mean. And maybe someone can tell me how to properly pass the date to pandas for the search. This is how I successfully use pandas to extract rows in an excel sheet if any cell in column emr_first_access_date is greater than or equal to '2019-09-08' I do this successfully with the following code: import pandas as pd HISorigFile = "C:\\folder\\inputfile1.xlsx" #opens excel worksheet df = pd.read_excel(HISorigFile, sheet_name='Non Live', skiprows=8) #locates the columns I want to write to file including date column emr_first_access_date if greater than or equal to '2019-09-08' data = df.loc[df['emr_first_access_date'] >= '2019-09-08', ['site_name','subs_num','emr_id', 'emr_first_access_date']] #sorts the data datasort = data.sort_values("emr_first_access_date",ascending=False) #this creates the file (data already sorted) in panda with date and time. datasort.to_excel(r'C:\\folder\sitesTestedInLastWeek.xlsx', index=False, header=True)However, the date above is hardcoded of course. So, I need the user running this script to input the date. I created a very basic working input prompt with the following: import datetime #prompts for input date TestedDateBegin = input('Enter beginning date to search for sites tested in YYYY-MM-DD format') year, month, day = map(int, TestedDateBegin.split('-')) date1 = datetime.date(year, month, day)Obviously I want to pass TestedDateBegin to pandas, changing the pertinent code line: data = df.loc[df['emr_first_access_date'] >= '2019-09-08', ['site_name','subs_num','emr_id', 'emr_first_access_date']]to something like: data = df.loc[df[b]['emr_first_access_date'] >= 'TestedDateBegin', ['site_name','subs_num','emr_id', 'emr_first_access_date']]Obviously this doesn't work. But how do I proceed? I am very new to programming so I not always clear how to proceed. Does the date inputted in TestedDateBegin need to be added to a return? Or should it be put in a single item list? What is the right approach? Thx! RE: Trying to Pass date to pandas search from input prompt - curranjohn46 - Oct-10-2019 This is resolved. I had to remove the single quotes around TestedDateBegin as python, of course, interpreted that as a string and not a variable. Simple! data = df.loc[df[b]['emr_first_access_date'] >= TestedDateBegin,['site_name','subs_num','emr_id', 'emr_first_access_date']] |