2013-02-01 47 views
1

我一直在作爲初學者工作一段時間。總體而言,我想讀取一個NetCDF文件並將多個(〜50)列(和17520個案例)導入到Pandas DataFrame中。目前我已經設置了4個變量列表,但我希望能夠以某種方式進行擴展。我做了一個開始,但是如何通過50個變量來實現這一點,任何幫助都會很棒。它可以使用下面的代碼來處理4個變量。我知道它不漂亮,所以歡迎,仍然在學習。將NetCDF文件導入Pandas DataFrame

我的另一個問題是,當我嘗試將numpy數組直接讀入Pandas DataFrame中時,它不起作用,而是創建了17520列大的DataFrame。它應該是另一種方式(轉置)。如果我創建一個系列,它工作正常。所以我不得不使用以下幾行來解決這個問題。甚至不知道它爲什麼起作用。任何更好的方法的建議?尤其是當它涉及到50個變量

d={vnames[0] :vartemp[0], vnames[1] :vartemp[1], vnames[2] :vartemp[2], vnames[3] :vartemp[3]} 
hs = pd.DataFrame(d,index=times) 

完整的代碼粘貼下面

非常感謝 傑森

import pandas as pd 
import datetime as dt 
import xlrd 
import numpy as np 
import netCDF4 


def excel_to_pydate(exceldate): 
    datemode=0   # datemode: 0 for 1900-based, 1 for 1904-based 
    pyear, pmonth, pday, phour, pminute, psecond = xlrd.xldate_as_tuple(exceldate, datemode) 
    py_date = dt.datetime(pyear, pmonth, pday, phour, pminute, psecond) 
    return(py_date) 

def main(): 
    filename='HowardSprings_2010_L4.nc' 
#Define a list of variables names we want from the netcdf file 
    vnames = ['xlDateTime', 'Fa', 'Fh' ,'Fg'] 

# Open the NetCDF file 
    nc = netCDF4.Dataset(filename) 

#Create some lists of size equal to length of vnames list. 
    temp=list(xrange(len(vnames))) 
    vartemp=list(xrange(len(vnames))) 

#Enumerate the list and assign each NetCDF variable to an element in the lists. 
# First get the netcdf variable object assign to temp 
# Then strip the data from that and add to temporary variable (vartemp) 
    for index, variable in enumerate(vnames):    
     temp[index]= nc.variables[variable] 
     vartemp[index] = temp[index][:] 

# Now call the function to convert to datetime from excel. Assume datemode: 0 
    times = [excel_to_pydate(elem) for elem in vartemp[0]] 

#Dont know why I cant just pass a list of variables i.e. [vartemp[0], vartemp[1], vartemp[2]] 
#But this is only thing that worked 
#Create Pandas dataframe using times as index 
    d={vnames[0] :vartemp[0], vnames[1] :vartemp[1], vnames[2] :vartemp[2], vnames[3] :vartemp[3]} 
    theDataFrame = pd.DataFrame(d,index=times) 

#Define missing data value and apply to DataFrame 
    missing=-9999 
    theDataFrame1=theDataFrame.replace({vnames[0] :missing, vnames[1] :missing, vnames[2] :missing, vnames[3] :missing},'NaN') 

main() 
+0

爲什麼不直接拿[轉](HTTP://pandas.pydata .ORG /大熊貓-文檔的/ dev /生成/ pandas.DataFrame.transpose.html)?? –

回答

1

您可以取代:

d = {vnames[0] :vartemp[0], ..., vnames[3]: vartemp[3]} 
hs = pd.DataFrame(d, index=times) 

hs = pd.DataFrame(vartemp[0:4], columns=vnames[0:4], index=times) 

話說,大熊貓可以直接讀取HDF5,因此,或許同樣是針對的netCDF(這是基於HDF5)真的......

+0

我相信你可以通過orient ='index'來獲得你想要的建築行爲,參見http://pandas.pydata.org/pandas-docs/stable/dsintro.html?highlight=orient – Jeff

+0

謝謝。我仍然有問題。似乎熊貓一直想要將列表「vnames []」作爲行而不是列導入。所以,當我按照你的建議做時,它會給出一個斷言錯誤:傳遞的錯誤數量(4對17520)。如果我嘗試pd.DataFrame(vartemp [0:4]),它將返回Int64Index:4項,0到3 列:17520項,0到17519.我要瘋了 – user1911866

+0

@ user1911866你可以提供一個小例子文件來演示這個? –

相關問題