2015-08-09 70 views
0

我有下面的代碼給我幾乎正是我想要的:輸出不必要的陣列,熊貓

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: #for every year in the range 
     if year in dfState['Year'].tolist(): #if the year is in the list of years for the selected state 
      value = dfState[(dfState.Year == year)] 
      countsList.append(value.Count.values) 
     else: 
      countsList.append(np.nan.values) 
    print countsList 
    return countsList 

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

的問題是,我出來就把應該是一個清單,但我得到了這個詞到處數組:

[array([ 5561.]), array([ 5588.]), array([ 6059.]), array([ 6043.]), array([ 5958.]), array([ 6566.]), array([ 7160.]), array([ 6780.]), array([ 7327.]), array([ 7585.]), array([ 7483.]), array([ 7635.]), array([ 7735.])] 

如何刪除數組中我輸出?

回答

1

熊貓的Dataframe將其數據存儲在numpy數組中。這就是爲什麼你在輸出中看到字數組的原因。如果你想將它轉換爲普通的Python列表而不是numpy數組,你可以調用tolist()

# untested 
for year in yearList: #for every year in the range 
    if year in dfState['Year'].tolist(): #if the year is in the list of years for the selected state 
     value = dfState[(dfState.Year == year)] 
     countsList.append(value.Count.values.tolist()) 
    else: 
     countsList.append(np.nan.values.tolist()) 
+0

冷靜,得到了它。謝謝! – madman

0

array是由NumPy的庫,它是一個用於Python的科學庫中創建的數據結構。人們可以用類似的方式從數組和列表中檢索項目。

由於value.Count.valuesnp.nan.values一個項目回報陣列,可以代替項追加到countsList直接:

countsList.append(value.Count.values[0]) 
... 
countsList.append(np.nan.values[0]) 

來源:http://docs.scipy.org/doc/numpy/reference/arrays.html