Python Forum

Full Version: pandas: can we look for the index of a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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']
I got, thanks