gpt4 book ai didi

python - csv 中缺少第一行

转载 作者:行者123 更新时间:2023-11-28 16:25:47 25 4
gpt4 key购买 nike

我正在将 .csv 文件读入数据框 (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 Cash

CorpActionsDf 负责人:

                       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())

有人知道我错过了什么吗?

谢谢

最佳答案

对于 header 参数,您必须使用 None 而不是 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'])        

关于python - csv 中缺少第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36811437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com