dataframe - Subsetting a data frame with dates as `str` and nan values in Python -


i have data frame, extracted .csv file using data = pandas.read_csv

one of columns of data frame dates, such '14/09/2015', type of data str.

i need create subset, use: newdataframe = dataframe['datescolumn'][dataframe['datescolumn']==desired date]

but have 2 main problems:

  1. since dates strings, have tried use slice [-1]. error: keyerror : -1l

i tried use code select 2014:

newdataframe = dataframe['datescolumn'][dataframe['datescolumn'][-1]==4]

  1. i have empty fields have been imported nan values. if try perform for loop transform data, error:

typeerror: 'float' object has no attribute '__getitem__'

q: how can subset data (or clean it) year?

many thanks.

for nan values can use fillna().

# fill nans zeros nonans = withnans.fillna(0) 

and date issue, instead of handling date strings should let existing libraries handle them you. in case read_csv() function can you. see documentation here.

here's little example:

csv file:

1,14/09/2016,dataa 1,14/09/2015,dataa 2,14/10/2014,dataa2 

code:

import pandas pd datetime import date  df = pd.read_csv("test.csv", header=none, parse_dates=[1]) df[df[1] > date.today()] 

prints only

   0          1      2 0  1 2016-09-14  dataa 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -