2016-04-23 80 views
3

我读.csv文件到一个数据帧(CorpActionsDf),但是当我打印CorpActionsDf的脑子里,我看到我丢失的数据的第一行:缺少第一行从CSV

头将该.cvs数据:

CorpActionsDf的
BBG.XAMS.ASML.S 24/04/2015 0.7 Annual Regular Cash 
BBG.XAMS.ASML.S 25/04/2014 0.61 Annual Regular Cash 
BBG.XAMS.ASML.S 26/04/2013 0.53 Annual Regular Cash 
BBG.XAMS.ASML.S 26/11/2012 9.18 None Return of Capital 
BBG.XAMS.ASML.S 27/04/2012 0.46 Annual Regular Cash 

头:

     date factor_value reference    factor 
unique_id                
BBG.XAMS.ASML.S 25/04/2014   0.61 Annual  Regular Cash 
BBG.XAMS.ASML.S 26/04/2013   0.53 Annual  Regular Cash 
BBG.XAMS.ASML.S 26/11/2012   9.18  None Return of Capital 
BBG.XAMS.ASML.S 27/04/2012   0.46 Annual  Regular Cash 
BBG.XAMS.ASML.S 26/04/2011   0.40 Annual  Regular Cash 

正如你所看到的数据在CSV的第一行是从数据帧丢失。

BBG.XAMS.ASML.S 24/04/2015 0.7 Annual Regular Cash 

我的代码如下:

def getCorpActionsData(rawStaticDataPath): 
    pattern = 'CorporateActions'+ '.csv' 
    staticPath = rawStaticDataPath 

    with open(staticPath+pattern,'rt') as f: 

     #staticDf=pd.read_csv(f,engine='c',header=0,index_col=0, parse_dates=True, infer_datetime_format=True,usecols=(0,3)) 
     CorpActionsDf=pd.read_csv(f,engine='c',header=0,index_col=0, parse_dates=True, infer_datetime_format=True,names=['unique_id', 'date','factor_value','reference','factor'])   
     print('CorpActionsDf') 
     print(CorpActionsDf.head()) 

任何一个有一个想法,我缺少的是什么?

感谢

回答

1

你试过标题=无不是头= 0?

的Docu说的头= 0:

“缺省行为,如果设置为0,如果没有名字通过,否则没有明确地传递标题= 0到能够取代现有名称。”

CorpActionsDf=pd.read_csv(f,engine='c',header=None,index_col=0, parse_dates=True, infer_datetime_format=True,names=['unique_id', 'date','factor_value','reference','factor']) 
2

你必须使用None,而不是0header参数。否则,请告诉代码将第0行视为包含标题的行,并且仅在后面用names参数替换它们。

CorpActionsDf=pd.read_csv(f,engine='c',header=None,index_col=0, parse_dates=True, infer_datetime_format=True,names=['unique_id', 'date','factor_value','reference','factor'])   
0

我不确定您是否正确使用参数。我不知道熊猫,因为我使用Numpy,但如果我看起来Pandas Documentation,我认为头和名称参数不好。

header = 0替换现有名称,因此您应该编写header = None

CorpActionsDf=pd.read_csv(f,engine='c',header=None,index_col=0, parse_dates=True, infer_datetime_format=True,names=['unique_id', 'date','factor_value','reference','factor']) 

试着说我是否更好?否则,你可以使用Numpy,我可以帮你!