vendredi 7 août 2015

Assign data from a data frame to a value and looping

I have a set of data, sample piece:

Year       State       Count 
1999     California     10
2000         -            - 
2001     California     20

I need to pull this from the dataframe and put it into the list, but need to account for the missing data for year 2000 (using the np.nan).

I am having issues with assign pieces from my dataframe as it is a view only, please see below and help, but with explanation.

def stateCountAsList(filepath,state):
    import pandas as pd 
    pd.set_option('display.width',200)

    import numpy as np 

    dataFrame = pd.read_csv(filepath,header=0,sep='\t')
    df = dataFrame.iloc[0:638,:]

    dfState = df[df['State'] == state]
    yearList = range(1999,2012)
    countsList = []

    for year in yearList: 
        if year in dfState['Year']:
            countsList = dfState['Count'].tolist()
    else: 
        countsList.append(np.nan)
    return countsList
    print countsList.tolist() 


stateCountAsList(filepath, state)
state = 'California'
filepath = 'United States Cancer Statistics, 1999-2011 Incidencet.txt' 

Aucun commentaire:

Enregistrer un commentaire