Home>

Let TimeAndValue be pd.DataFrame.

here,
CurrentValue = TimeAndValue.tail (1)
As, I took out the data of the last row as CurrentValue in the form of pandas.Series.

print (CurrentValue.iloc [0, 0: 6])
Then, the data of the first 6 data plus indexes will be printed line by line.

However, if you do as below,
print (CurrentValue.iloc [0, 0])
print (CurrentValue.iloc [0, 1])
print (CurrentValue.iloc [0, 2])
print (CurrentValue.iloc [0, 3])
print (CurrentValue.iloc [0, 4])
print (CurrentValue.iloc [0, 5])
print (CurrentValue.iloc [0, 6])

The final print (CurrentValue.iloc [0, 6]) is IndexError: single positional indexer is out-of-bounds.

I don't understand the reason ...
Is it possible to retrieve only the last index without using a different method?
Instead, if it is iloc [0: 6], it will spit out everything without error.

I would appreciate it if you could explain it.

  • Answer # 1

    [0: 6] is a slice from index 0 to index 5. Index 6 is not included in the sliced ​​result.
    Index 6 is out of range for an array with only 6 elements.

    The same is true for Series.iloc, as described in the list example.

    x = [1, 2, 3]
    print (x [: 2]) # [1, 2], sliced ​​result does not include x [2]