Python Forum
pandas: can we look for the index of a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: pandas: can we look for the index of a string (/thread-20164.html)



pandas: can we look for the index of a string - paul18fr - Jul-30-2019

Dear All

For the first time I'm trying to use Pandas to load and read csv files that inlude values and strings; DataFrames object is quite new for me and I'm still trying to understand its specificities.

I'm wondering if I can look for strings (and to get the row indexes for example) as I do for values using numpy.where; the pandas.dataFrame.where shows examples using mainly values, but not any string, but is it possible?

obviously the following example does not work, but highlight I guess what I'm trying to perform.
loc_USA = pd.DataFrame.where(df['country'] == 'USA')
loc_USA = pd.DataFrame.where(df[:,1] == 'USA')
Thanks for any suggestion

Paul

PS: the following is not related to the previous question, but I'm wondering from where the comment beneath my nickname comes from Huh (not really ethic)


RE: pandas: can we look for the index of a string - scidam - Jul-31-2019

If you ever look at the official docs, you can find that pandas .where method is
primarily used to change the data:

Quote:Replace values where the condition is False.


You probably want to consider a boolean array:

df['country'] == 'USA'
or access rows using:
df[df['country'] == 'USA']



RE: pandas: can we look for the index of a string - paul18fr - Jul-31-2019

I got, thanks