How do I filter data by date on a dataframe (Python)

Asked

Viewed 1,889 times

2

Since you would have to create a new dataframe with the information coming from these dates below.

import pandas as pd
import numpy as np
import datetime
%matplotlib inline
races = pd.read_csv('races.csv')
results = pd.read_csv('results.csv')
last10M = pd.merge(results, races, how='outer', on='raceId')
start = datetime.datetime(int(2008), int(4), 20)
end = datetime.datetime(int(2008), int(4), 20)

I’m stuck in this last part, the dates I’ve already done just that I need to filter through the 'last10M' dataframe. The dataframe 'last10M' would have to be filtered in the field 'date' from 2008 to 2018

  • You can give an example (few lines) of your Results.csv and Races.csv pff, to tailor the response as much as possible to your case

  • Both are here https://www.kaggle.com/cjgdev/formula-1-race-data19502017/data

  • I would have to in the dataframe 'last10M' in the 'date' column filtered from 2008 to 2018

1 answer

3


Try the following:

races = pd.read_csv('races.csv', parse_dates=['date']) # tratar a coluna date como datetime
results = pd.read_csv('results.csv')
last10M = pd.merge(results, races, how='outer', on='raceId')
interval = (last10M['date'] > '2008-01-01') & (last10M['date'] <= '2018-01-01')
df_interval = last10M.loc[interval]
  • 'df' is not defined

  • Basically I forgot parse_dates to create the expression. Thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.