KeyError: 0 访问 pandas 系列中的值时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46153647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
KeyError: 0 when accessing value in pandas series
提问by Bharat Sharma
In my script I have df['Time'] as shown below.
在我的脚本中,我有 df['Time'] ,如下所示。
497 2017-08-06 11:00:00
548 2017-08-08 15:00:00
580 2017-08-10 04:00:00
646 2017-08-12 23:00:00
Name: Time, dtype: datetime64[ns]
But when i do
但是当我这样做的时候
t1=pd.Timestamp(df['Time'][0])
I get an error like this :
我收到这样的错误:
KeyError: 0
密钥错误:0
Do I need any type conversion here, if yes then how it can be fixed?
我是否需要在这里进行任何类型转换,如果是,那么如何修复?
回答by cs95
You're looking for df.iloc
.
您正在寻找df.iloc
.
df['Time'].iloc[0]
df['Time'][0]
would've worked if your series had an index beginning from 0
df['Time'][0]
如果您的系列的索引从 0
And if need scalar only use Series.iat
:
如果只需要标量,请使用Series.iat
:
df['Time'].iat[0]