can I split the value in a pandas row for searching?

Viewed 152

I'm searching a DF using:

df.loc[df['Ticker'] == 'ibm'

The problem is df['Ticker'] is formated with another value after it(for example 'ibm US').

normally for string I can do something like .split[" "][0] to find the match but it doesn't work for my pandas search above(df.loc[df['Ticker'].split[" "][0] == 'ibm' - fails with AttributeError: 'Series' object has no attribute 'split').

What can I do to achieve my goal?

1 Answers

Are you are looking for str.contains?:

new_df = df[df['Ticket'].str.contains(r'ibm',case=False)]

which will create a new dataframe from rows that the 'Ticker' column contains 'ibm'.

You can use or and case=False (case insensitive) in str.contains:

new_df = df[df['Ticket'].str.contains(r'ibm|msft|google|..',case=False)]
Related