0

I was trying some coding as practice where I found the following:

df = pd.read_csv('stock.csv',usecols=[1,2,3,4])
df = df.iloc[::-1]

I am not able to understand what the second statement is doing. I tried to read the documentation for iloc but am not able to understand the purpose of it.

Please help me understand the reason behind the usage of second line in the code.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • This will reverse the order of dataframe, iloc works like slicing and -1 means to traverse in backward direction – akash karothiya Jul 12 '18 at 09:28
  • The syntax is akin to Python list or NumPy array slicing. I've marked this a duplicate which explains in more detail. If this is not sufficient, let me know and I can reopen if you can explain specifically the problem. – jpp Jul 12 '18 at 09:29
  • Looks like a line to reverse the data. But I think it could be done differently – Anton vBR Jul 12 '18 at 09:29
  • Does it means that it reverses the last column values of the `df`? or it reverse the coumns of teh `df`? – Jaffer Wilson Jul 12 '18 at 09:30
  • @JafferWilson, `df.iloc[x]` or `df.iloc[x, :]` slices by row only, `df.iloc[:, y]` slices by column only, `df.iloc[x, y]` slices by row & column. – jpp Jul 12 '18 at 09:31
  • @jpp Nah .. the question you have suggested is related to slicing of the arrays. But my question is related to `iloc`. What for it is? I can understand that it is dealing with the last value as the statement`[::-1]` but why with `iloc`? – Jaffer Wilson Jul 12 '18 at 09:33
  • @JafferWilson, Why *not* with `iloc`? The same syntax can be used across lists, arrays, dataframe indexers. This should be *expected*, not unusual. – jpp Jul 12 '18 at 09:34

0 Answers0