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 D ate.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])
|