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
#1
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.
Reply


Messages In This Thread
List of dataframe values beginning with x,y or z - by glidecode - Nov-08-2021, 08:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  remove duplicates from dicts with list values wardancer84 27 721 May-27-2024, 04:54 PM
Last Post: wardancer84
  Just beginning dalekeel 2 356 Apr-15-2024, 11:49 AM
Last Post: Pedroski55
  Copying the order of another list with identical values gohanhango 7 1,295 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,356 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,301 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Transposing a dataframe without creating NaN values doug2019 2 1,053 Mar-18-2023, 03:14 PM
Last Post: jefsummers
  Program doesnt return beginning bilisim19 2 993 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  Adding values with reduce() function from the list of tuples kinimod 10 2,848 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,151 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  function returns dataframe as list harum 2 1,533 Aug-13-2022, 08:27 PM
Last Post: rob101

Forum Jump:

User Panel Messages

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