Bottom Page

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")
buran wrote Nov-15-2017, 07:59 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info. This time I added code tags for you.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to extract data from paragraph using Machine Learning with python? bccsthilina 1 250 Jun-19-2020, 01:55 AM
Last Post: jefsummers
  Select column between to dates CSV data PythonJD 0 265 Apr-14-2020, 12:22 PM
Last Post: PythonJD
  python pandas: diff between 2 dates in a groupby bluedragon 0 864 Mar-25-2020, 04:18 PM
Last Post: bluedragon
  how to extract financial data from photocopy of document angela1 6 342 Feb-15-2020, 05:50 PM
Last Post: jim2007
  Python numpy fft from data file magnet1 1 291 Feb-06-2020, 07:30 AM
Last Post: magnet1
  How to extract data between two strings SriMekala 2 504 Aug-08-2019, 01:54 PM
Last Post: SriMekala
  Learning indexing with python, pick dates dervast 1 401 Jul-11-2019, 07:29 AM
Last Post: scidam
  How to extract different data groups from multiple CSV files using python Rafiz 3 762 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  OpenCV - extract 1st frame out of a video file kerzol81 2 5,208 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  Upload csv file as numbers (floating?) and extract element, row, and column bentaz 7 1,537 Mar-19-2018, 05:34 PM
Last Post: bentaz

Forum Jump:


Users browsing this thread: 1 Guest(s)