Nov-08-2021, 08:30 PM
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.
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.