Python Forum
pandas: can we look for the index of a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas: can we look for the index of a string
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


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Getting Index Error - list index out of range krishna 2 855 Jan-09-2021, 08:29 AM
Last Post: buran
  How to search for specific string in Pandas dataframe Coding_Jam 1 957 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  Getting Index Error - list index out of range RahulSingh 2 3,130 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 1,157 Jan-28-2020, 09:30 AM
Last Post: axelle
  Parse XML String in Pandas Dataframe creedX 2 3,186 Dec-09-2019, 07:35 PM
Last Post: creedX
  How to speed up work with pandas index? AlekseyPython 1 962 Oct-16-2019, 02:06 PM
Last Post: AlekseyPython
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 1,418 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  Simple String to Time within a pandas dataframe Ecniv 1 1,355 Jun-14-2019, 03:25 AM
Last Post: scidam
  Converting string the pandas dataframe chrismc 0 1,367 Jan-24-2019, 11:07 AM
Last Post: chrismc
  How to get first and last row index of each unique names in pandas dataframe SriRajesh 1 3,089 Oct-13-2018, 07:04 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020