我有一个带日期和两个输入值的csv文件。在这里,我需要读取第一列中包含值的日期。在这里,我使用了代码,它给了我这个错误“'numpy.int64'对象没有属性'loc'”
这是我的代码:
data = pd.read_csv("data6.csv")
data['date']= pd.to_datetime(data['date'] + " " + data['time'].str.strip(), format='%d/%m/%Y %H:%M:%S')
filtered = data['X']
current_X = filtered.iloc[0]
current_time = filtered.iloc[0].loc['date']
错误:
AttributeError Traceback (most recent call last)
<ipython-input-24-b3a8e880770f> in <module>()
1 filtered = data['x']
2 current_x = filtered.iloc[0]
----> 3 current_time = filtered.iloc[0].loc['date']
AttributeError: 'numpy.int64' object has no attribute 'loc'
我的csv文件:
date time x x1
8/6/2018 6:15:00 141 0
8/6/2018 6:45:00 0 20
8/6/2018 7:45:00 0 0
8/6/2018 9:00:00 0 0
8/6/2018 9:25:00 95 30
8/6/2018 9:30:00 0 0
8/6/2018 11:00:00 149 0
8/6/2018 11:30:00 0 0
8/6/2018 13:30:00 0 40
8/6/2018 13:50:00 85 0
8/6/2018 15:00:00 0 0
8/6/2018 15:25:00 0 0
答案 0 :(得分:1)
有2种可能的解决方案-用Index.get_loc
的位置选择DataFrame.iloc
的date
列的位置:
current_time = data.iloc[0, data.columns.get_loc('date')]
或获取第一个索引值的标签,然后按DataFrame.loc
进行选择:
current_time = data.loc[data.index[0], 'date']
如果有默认的RangeIndex:
current_time = data.loc[0, 'date']
您的解决方案无效,因为:
#returned Series
filtered = data['X']
#returned first value of Series - scalar
current_X = filtered.iloc[0]
#error
current_time = filtered.iloc[0].loc['date']