I have a python Data Frame how can I Find the Index number of any Label.
df.loc['Brazil']
I have the Label but I want to know the index number so I can use the df.iloc[] method.
I have a python Data Frame how can I Find the Index number of any Label.
df.loc['Brazil']
I have the Label but I want to know the index number so I can use the df.iloc[] method.
Assuming this input:
df = pd.DataFrame({'col': None}, index=['Argentina', 'Brazil', 'Columbia']) col Argentina None Brazil None Columbia None You can use get_loc: df.index.get_loc('Brazil') -> outputs 1
However, this assumes a non duplicated index. If you have:
col Argentina None Brazil None Columbia None Brazil None This will output: array([False, True, False, True]).
In this case, you can use the nonzero method of the output numpy array:
>>> df.index.get_loc('Brazil').nonzero()[0] array([1, 3])