Python Forum
Extract data between two dates from a .csv file using Python 2.7 - 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: Extract data between two dates from a .csv file using Python 2.7 (/thread-6310.html)



Extract data between two dates from a .csv file using Python 2.7 - sujai_banerji - Nov-15-2017

I have an Excel file with the headings: Date, AQI and Raw Conc. The date is in yyyy-mm-dd format. I want to extract the data between two dates and copy it to another Excel file via Python.

What I have tried:

import pandas as pd
from pandas import DataFrame
 
import datetime
import pandas.io.data
 
df = pd.read_csv(r"C:\Users\Win-8.1\Desktop\delhi\dated.csv", index_col = 'Date', parse_dates = True)
 
mask = (df['Date'] >= '09-01-2015') & (df['Date'] <= '11-30-2016')
 
mask.to_csv(r"C:\Users\Win-8.1\Desktop\delhi\extracted.csv")



RE: Extract data between two dates from a .csv file using Python 2.7 - snippsat - Nov-15-2017

Try lower case Date.
Test with you mask line,i generate dates from 2013 to 2017.
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((60,3)))
df['date'] = pd.date_range('2013-1-1', periods=60, freq='M')
mask = (df['date'] >= '09-01-2015') & (df['date'] <= '11-30-2016')
print(df.loc[mask])
Output:
           0         1         2       date 32  0.237158  0.571215  0.375273 2015-09-30 33  0.277659  0.177575  0.897463 2015-10-31 34  0.978287  0.738475  0.241821 2015-11-30 35  0.715597  0.020696  0.095387 2015-12-31 36  0.170725  0.849866  0.113690 2016-01-31 37  0.433582  0.111436  0.783705 2016-02-29 38  0.718041  0.787051  0.247592 2016-03-31 39  0.183515  0.932703  0.924351 2016-04-30 40  0.425601  0.992360  0.160537 2016-05-31 41  0.255489  0.499571  0.628678 2016-06-30 42  0.830537  0.161014  0.180552 2016-07-31 43  0.136601  0.528890  0.074930 2016-08-31 44  0.878454  0.035144  0.066101 2016-09-30 45  0.373270  0.756207  0.076302 2016-10-31 46  0.750792  0.467861  0.431725 2016-11-30