Selecting Line of Pandas DataFrame by a String Index

Viewed 3478

I have a pandas dataframe of the form:

enter image description here

Where "it", "their" and "charact" are the indexes. How can I select a value based on the index? When I try the following:

corpus_df.iloc['it',1]

I get an error:

ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
2 Answers

You can use corpus_df.loc['it'][1] instead

The correct form is: corpus_df.loc['it', 1]

There are two different properties: loc and iloc
iloc is used for integer position based indexing.
loc is used for label based indexing, therefore you can use it with string index value.

Related