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 between two strings SriMekala 2 245 Aug-08-2019, 01:54 PM
Last Post: SriMekala
  Learning indexing with python, pick dates dervast 1 225 Jul-11-2019, 07:29 AM
Last Post: scidam
  How to extract different data groups from multiple CSV files using python Rafiz 3 424 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  OpenCV - extract 1st frame out of a video file kerzol81 2 2,461 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  Upload csv file as numbers (floating?) and extract element, row, and column bentaz 7 1,226 Mar-19-2018, 05:34 PM
Last Post: bentaz
  I'm working onn below code to extract data from excel using python kiran 1 1,144 Oct-24-2017, 01:42 PM
Last Post: kiran
  Problem of getting XLS file data into python klllmmm 3 8,463 Jan-19-2017, 08:59 AM
Last Post: klllmmm
  Identifying items in a csv file that also appear in a Text extract Jaynorth 17 5,906 Sep-21-2016, 10:51 PM
Last Post: Jaynorth

Forum Jump:


Users browsing this thread: 1 Guest(s)