1

I have a csv file with date and two input values. Here I need to read date with value contain in first column. here I used the code and it gave me this error"'numpy.int64' object has no attribute 'loc'"

Here is my code:

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']

error:

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'

my csv file :

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

1
  • filtered is Series, contains only X column data. And you are trying to get date, which doesn't exists in it. Commented Oct 15, 2019 at 8:57

1 Answer 1

1

There are 2 possible solutions - select by positions with Index.get_loc for position of date column with DataFrame.iloc:

current_time = data.iloc[0, data.columns.get_loc('date')]

Or get label of first index value and select by DataFrame.loc:

current_time = data.loc[data.index[0], 'date']

If there is default RangeIndex:

current_time = data.loc[0, 'date']

Your solution not working, because:

#returned Series
filtered = data['X']
#returned first value of Series - scalar
current_X = filtered.iloc[0]
#error
current_time = filtered.iloc[0].loc['date']
Sign up to request clarification or add additional context in comments.

4 Comments

Is it tested? Please look at my comment under question.
@shaikmoeed - oops, you are right, need data instead filtered
@jezrael I think your answer is correct. But I have a doubt , if I want to read the date of second column input value, then how to read it?
@jezrael ya I got it. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.