Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas restricting csv read to certain rows
#1
Dear Pandas Experts,

I am tryig to extract data from a .csv file that contains columns called CarId, IssueDate

import pandas as pd
    train = pd.read_csv('train.csv', index_col=False, encoding="ISO-8859-1")
The issue date is of format "mm/dd/yyyy".
I want to get only those rows that have a year between 2012 and 2016.

Can someone help with that? I have no idea how to make this efficient i.e
maybe filtering before all the data is extracted.
Quote
#2
You should look here:
https://stackoverflow.com/questions/1746...nize-dates
https://stackoverflow.com/questions/2937...-two-dates

import pandas as pd


df = pd.read_csv('dates.csv', delimiter=';', parse_dates=['date'])
print(df.dtypes)
mask =(df['date'] > pd.Timestamp(2012,1,1)) & (df['date'] < pd.Timestamp(2016,1,1))
print(df[mask])
My code examples are always for Python 3.x.
All humans together. We don't need politicians!
Quote
#3
Hi Dead_Eye,

Many thanks for your reply.
I tried what you suggested but even though there are years in range, nothing gets extracted.


    mask =(pd.DatetimeIndex(train_df['ticket_issued_date']).year > 2012) & (pd.DatetimeIndex(train_df['ticket_issued_date']).year < 2016)
    print(train_df[mask]) # is empty

    train_df['yearcolumn'] = pd.DatetimeIndex(train_df['ticket_issued_date']).year
    print(train_df['yearcolumn'].unique())
    #output [2004 2005 2006 2007 1938 1963 1988 2008 2009 2010 2011]

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Dropping rows in a dataframe sobrio1 0 40 Dec-03-2017, 11:15 PM
Last Post: sobrio1
  Problem with updata the data into the database as it went into the wrong rows chris0147 5 117 Nov-11-2017, 07:03 PM
Last Post: Larz60+
  Insert csv data rows into a tree widget LMP2016 6 139 Nov-10-2017, 03:51 AM
Last Post: Larz60+
  deleting certain rows from multidimensional list aster 4 137 Nov-05-2017, 10:52 PM
Last Post: DeaD_EyE
  Stack dataframe columns into rows klllmmm 0 159 Sep-03-2017, 02:26 AM
Last Post: klllmmm
  [split] Rearranging CSV columns and rows Ivan1 1 118 Aug-31-2017, 01:41 AM
Last Post: Ivan1
  Need help with Pandas evev 5 355 Aug-10-2017, 09:57 AM
Last Post: evev
  Select rows based on a criteria klllmmm 8 900 May-11-2017, 06:58 PM
Last Post: klllmmm
  Iterating through rows and saving result in a new column kiton 4 647 Apr-21-2017, 03:13 PM
Last Post: kiton
  how to select particular rows data from a array raady07 3 476 Mar-06-2017, 02:21 AM
Last Post: raady07

Forum Jump:


Users browsing this thread: 1 Guest(s)