Python Forum
List of dataframe values beginning with x,y or z
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of dataframe values beginning with x,y or z
I have found a way to return all rows of a specified column in a data frame beginning with letter x.
The following will return a new dataframe reduced to the specified column and for row where the expression evaluates to True:

sorted = rawdata[rawdata['Ticker'].astype(str).str[0]=="A"]

However, when I need to check that the first letter is either of [x,y or z] I get stuck.

I have tried:

sorted = rawdata[rawdata['Ticker'].astype(str).str[0]=="A" | "B"]
sorted = rawdata[rawdata['Ticker'].astype(str).str[0]==["A" | "B"]]
sorted = rawdata[rawdata['Ticker'].astype(str).str[0]=="A" | rawdata['Ticker'].astype(str).str[0]=="B"]

As I have spent hours on this problem, I'd be very interested to know what the logic is behind the way Python/Pandas works here.
I'm surprised that there isn't a simple pandas function for this, similar to SQL query style.
Did you try using "in", as in ..... in ['x','y','z']
It then gives me this error (which I have run into many times today):
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I figured out the problem!

It is described here:

Simply, the expression is interpreted with priority to the "|" - so in my example, it will look to see if the first letter is equal to A or equal to the rest of the expression. Hope this makes sense to anyone looking for the same.

I fixed it like this - simply adding two sets of parentheses:

sorted = rawdata[(rawdata['Ticker'].astype(str).str[0] =="A") | (rawdata['Ticker'].astype(str).str[0] =="B")]

Possibly Related Threads…
Thread Author Replies Views Last Post
  Just beginning dalekeel 2 203 Apr-15-2024, 11:49 AM
Last Post: Pedroski55
  Copying the order of another list with identical values gohanhango 7 1,140 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,237 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,156 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Transposing a dataframe without creating NaN values doug2019 2 983 Mar-18-2023, 03:14 PM
Last Post: jefsummers
  Program doesnt return beginning bilisim19 2 934 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  Adding values with reduce() function from the list of tuples kinimod 10 2,648 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,069 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  function returns dataframe as list harum 2 1,402 Aug-13-2022, 08:27 PM
Last Post: rob101
  Using .append() with list vs dataframe Mark17 7 10,417 Jun-12-2022, 06:54 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

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