Python Forum
Extract data between two dates from a .csv file using Python 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract data between two dates from a .csv file using Python 2.7
#1
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")
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add data to CSV file via Python Anaconda23 0 721 Dec-30-2022, 02:31 AM
Last Post: Anaconda23
  Training a model to identify specific SMS types and extract relevant data? lord_of_cinder 0 978 Oct-10-2022, 04:35 AM
Last Post: lord_of_cinder
  extract and plot data from a txt file usercat123 2 1,230 Apr-20-2022, 06:50 PM
Last Post: usercat123
  how to handling time series data file with Python? aupres 4 2,991 Aug-10-2020, 12:40 PM
Last Post: MattKahn13
  How to extract data from paragraph using Machine Learning with python? bccsthilina 2 3,044 Jul-27-2020, 07:02 AM
Last Post: hussainmujtaba
  Select column between to dates CSV data PythonJD 0 1,786 Apr-14-2020, 12:22 PM
Last Post: PythonJD
  python pandas: diff between 2 dates in a groupby bluedragon 0 3,282 Mar-25-2020, 04:18 PM
Last Post: bluedragon
  how to extract financial data from photocopy of document angela1 6 3,680 Feb-15-2020, 05:50 PM
Last Post: jim2007
  Python numpy fft from data file magnet1 1 2,755 Feb-06-2020, 07:30 AM
Last Post: magnet1
  How to extract data between two strings SriMekala 2 2,398 Aug-08-2019, 01:54 PM
Last Post: SriMekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020