Pandas and Date: problem with operator.How to resolve - 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: Pandas and Date: problem with operator.How to resolve (/thread-18305.html) |
Pandas and Date: problem with operator.How to resolve - frame - May-12-2019 Hi. I have a file csv (es10.csv) like this: Note: Date = day**month**YearNow, i would like to get the following output: Note: the value of the date must be greater than 30/11/2014My code is: df=pd.read_csv("esv10.csv",encoding='windows-1252',sep="\t",decimal=',',keep_default_na=False, na_values=[''], converters={"Conc.":str},) pd.options.display.float_format = '{:,.0f}'.format myfilter=df["Date"]>"30/11/2014" df= df.where(myfilter ,axis=1).dropna() print(df)If i use the operator ==, output = 8928 30/11/2014 3 15 Is it possible to use operator: > or < or =!? Thanks RE: Pandas and Date: problem with operator.How to resolve - Yoriz - May-12-2019 I changed the sep="\t" to sep=" " and added df['Date'] = pd.to_datetime(df['Date']) to turn the date column into a datetime typeimport pandas as pd df=pd.read_csv("some_data.csv",encoding='windows-1252',sep=" ",decimal=',',keep_default_na=False, na_values=[''], converters={"Conc.":str},) pd.options.display.float_format = '{:,.0f}'.format df['Date'] = pd.to_datetime(df['Date']) myfilter=df["Date"]>"30/11/2014" df= df.where(myfilter ,axis=1).dropna() print(df)
RE: Pandas and Date: problem with operator.How to resolve - frame - May-13-2019 Thanks Yoriz, but the problem is format column date and transform it like this: 8929 18/12/2014 4 80 (day-month-year) ........ RE: Pandas and Date: problem with operator.How to resolve - snippsat - May-13-2019 Use dt.strftime to covert to wanted format. >>> import pandas as pd >>> df = pd.read_clipboard() >>> df['Date'] = pd.to_datetime(df['Date']) >>> df Conc. Date I II 3 8929 2014-12-18 4 80 4 8930 2014-12-28 8 60 5 8931 2014-12-31 9 12 >>> df['Date'] = df['Date'].dt.strftime('%d/%m/%Y') >>> df Conc. Date I II 3 8929 18/12/2014 4 80 4 8930 28/12/2014 8 60 5 8931 31/12/2014 9 12 RE: Pandas and Date: problem with operator.How to resolve - frame - May-13-2019 Thanks Snippsat. The conversion work, but applying "df.where" to filter df["Date"]>"30/11/2014"", the result does not change. Is valid the operator > or i do to use only operator ==? RE: Pandas and Date: problem with operator.How to resolve - snippsat - May-13-2019 (May-13-2019, 04:05 PM)frame Wrote: The conversion work, but applying "df.where" to filter df["Date"]>"30/11/2014"", the result does not change. Is valid the operator > or i do to use only operator ==?The code for @Yoriz should work,is that what you have problem with or are doing something else? Doing myfilter = df["Date"] == "30/11/2014" it will only drop. Test of code:>>> import pandas as pd >>> df = pd.read_clipboard() >>> df['Date'] = pd.to_datetime(df['Date']) >>> df Conc. Date I II 0 8926 2014-11-18 1 22 1 8927 2014-11-20 2 33 2 8928 2014-11-30 3 15 3 8929 2014-12-18 4 80 4 8930 2014-12-28 8 60 5 8931 2014-12-31 9 12 >>> myfilter = df["Date"] > "30/11/2014" >>> df = df.where(myfilter ,axis=1).dropna() >>> df['Date'] = df['Date'].dt.strftime('%d/%m/%Y') >>> df Conc. Date I II 3 8929.0 18/12/2014 4.0 80.0 4 8930.0 28/12/2014 8.0 60.0 5 8931.0 31/12/2014 9.0 12.0 RE: Pandas and Date: problem with operator.How to resolve - frame - May-13-2019 Now it works very well, Thanks for your patience Snippsat and Yoriz |