熊猫:迭代df行会产生奇怪的结果

时间:2015-08-21 14:47:38

标签: python-2.7 pandas

我正在阅读文字文件,看起来像这样的* 2013 6 1 0 0 0.00000000我收集有关时间的信息。这是我的代码:

import pandas as pd
from datetime import datetime

def readGPS(location):
    df1 = pd.DataFrame()
    with open(location) as handler:
        for i, line in enumerate(handler):
            if '*  2' in line:
                #Grab Timestamp
                links = line.split()
                index = datetime.strptime(' '.join(links[1:7]), '%Y %m %d %H %M %S.%f00')
            elif 'P ' in line:
                #Grab coordinates
                link = line.split()
                nr = int(link[1])
                satX = float(link[2])
                satY = float(link[3])
                satZ = float(link[4])
                dff = pd.DataFrame([[index, str(nr), satX, satY, satZ]], columns=['Time', 'Nr_gps', 'X_gps', 'Y_gps', 'Z_gps'])
                df1 = df1.append(dff)
    return df1

我需要迭代Time列中的行。

当我做print df1['Time']时一切正常,我得到了这个:

2013-06-01 00:00:00
2013-06-01 00:00:00
2013-06-01 00:00:00
2013-06-01 00:00:00
2013-06-01 00:05:00
2013-06-01 00:05:00
2013-06-01 00:05:00
Name: Time, dtype: datetime64[ns]

但是当我尝试这样做时

   for i in range(len(df1)):
        print df1.at[i, 'Time']

我得到这样的东西:

['2013-06-01T02:00:00.000000000+0200' '2013-06-01T02:00:00.000000000+0200'
 '2013-06-01T02:00:00.000000000+0200' '2013-06-01T02:00:00.000000000+0200'
 '2013-06-01T02:00:00.000000000+0200' '2013-06-01T02:00:00.000000000+0200'
 '2013-06-01T02:00:00.000000000+0200' '2013-06-01T02:00:00.000000000+0200'
 '2013-06-01T02:00:00.000000000+0200' '2013-06-01T02:05:00.000000000+0200'

我想检查每一行的小时数,我想我可以这样做df1.at[i, 'Time'].hour但它不起作用。你有什么想法吗?

0 个答案:

没有答案