熊猫给存在的元素带来关键错误

时间:2019-10-09 20:11:20

标签: python pandas

我有一个由2列,时间和1天活跃用户组成的Google服务数据集。我将时间列设置为索引,并使用pd.to_datetime对其进行了解析。当我尝试为时间列中的元素建立索引时,它会出现键错误。我导入了熊猫,datetime,dateutil.parser。我是python的新手,所以对它的可能感到困惑。

这是数据集的样子:

score=input('What is your score?')
Scorefile=open('score.txt','a')
Scorefile.write(score)
Scorefile.write("\n")
Scorefile.close()
Scorefile = open('score.txt','r')
with open('score.txt','r') as Scorefile:
     scores=Scorefile.readline()
     List=[]
     while scores:
           scores2=(scores.strip())
           int(scores2)
           List.append(scores2)
           scores=Scorefile.readline()
List.sort()
print(List)
#Output(not in ascending order)
['12', '12', '12', '12', '12', '13', '15', '17', '4', '5', '6']

代码:

                            1day_active_users
                     time   
2015-03-23 00:00:00-04:00             19687.0
2015-03-24 00:00:00-04:00             19437.0

错误消息:KeyError:'2015'

0 个答案:

没有答案