Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(pandas) access all rows except for the first 3 for the specific columns at index

I want to access all rows except for the first 3 for the specific columns at index 1, 2, 4, 5, 7, 8, 10, 11, 13, 14 of a csv file. How can I do this? All examples I have found show how to slice (for example 1:14 but I do not want all columns in between but specific ones.

When I try:

   p = df[3:, [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]]

I get an error:

    p = df[3:, [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]]
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/frame.py", line 2139, in __getitem__
    return self._getitem_column(key)
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/frame.py", line 2146, in _getitem_column
    return self._get_item_cache(key)
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/generic.py", line 1840, in _get_item_cache
    res = cache.get(item)
TypeError: unhashable type: 'slice'

and it does not work with the notation p = df[[3:], [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]]

like image 763
dusa Avatar asked Oct 19 '25 11:10

dusa


1 Answers

IIUC you need DataFrame.iloc for filter by positions here all rows without first 3 and columns names by positions:

df.iloc[3:, [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]]
like image 98
jezrael Avatar answered Oct 21 '25 09:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!