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
#1
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)
Reply
#2
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']
Reply
#3
I got, thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 606 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  [split] Getting Index Error - list index out of range krishna 2 2,565 Jan-09-2021, 08:29 AM
Last Post: buran
  How to search for specific string in Pandas dataframe Coding_Jam 1 2,415 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  Getting Index Error - list index out of range RahulSingh 2 6,100 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 2,549 Jan-28-2020, 09:30 AM
Last Post: axelle
  Parse XML String in Pandas Dataframe creedX 2 6,821 Dec-09-2019, 07:35 PM
Last Post: creedX
  How to speed up work with pandas index? AlekseyPython 1 2,157 Oct-16-2019, 02:06 PM
Last Post: AlekseyPython
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 2,615 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  Simple String to Time within a pandas dataframe Ecniv 1 2,480 Jun-14-2019, 03:25 AM
Last Post: scidam
  Converting string the pandas dataframe chrismc 0 2,333 Jan-24-2019, 11:07 AM
Last Post: chrismc

Forum Jump:

User Panel Messages

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