TypeError: unhashable type: 'slice' for pandas

前端 未结 4 2195
甜味超标
甜味超标 2021-02-19 14:27

I have a pandas datastructure, which I create like this:

test_inputs = pd.read_csv(\"../input/test.csv\", delimiter=\',\')

Its shape

         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 15:19

    Indexing in pandas is really confusing, as it looks like list indexing but it is not. You need to use .iloc, which is indexing by position

    print(test_inputs.iloc[100:200, :])
    

    And if you don't use column selection you can omit it

    print(test_inputs.iloc[100:200])
    

    P.S. Using .loc (or just []) is not what you want, as it would look not for the row number, but for the row index (which can be filled we anything, not even numbers, not even unique). Ranges in .loc will find rows with index value 100 and 200, and return the lines between. If you just created the DataFrame .iloc and .loc may give the same result, but using .loc in this case is a very bad practice as it will lead you to difficult to understand problem when the index will change for some reason (for example you'll select some subset of rows, and from that moment the row number and index will not be the same).

提交回复
热议问题