我正在将一个.csv文件读取到dataframe ( CorpActionsDf )中,但是当我打印CorpActionsDf的头部时,我发现丢失了第一行数据:
.cvs数据主管:
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 CashCorpActionsDf主管:
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中的第一行数据从dataframe中丢失。
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())有人知道我错过了什么吗?
谢谢
发布于 2016-04-23 13:33:24
你试过header=None而不是header=0吗?
Docu为header=0写的:
“如果没有传递名称,则默认行为设置为0,否则不传递。显式地传递header=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']) 发布于 2016-04-23 13:35:11
对于None参数,您必须使用0而不是0。否则,您告诉代码将第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']) 发布于 2016-04-23 13:35:53
我不确定你是否很好地使用了参数。我不认识熊猫,因为我使用的是Numpy,但是如果我看熊猫文献,我认为标题和名称参数都不是很好。
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,我可以帮你!
https://stackoverflow.com/questions/36811437
复制相似问题