Skip to content

Instantly share code, notes, and snippets.

@chicago-joe
Created March 17, 2020 14:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chicago-joe/5e1ba42d4013a4d6b0639b2b7c12eab2 to your computer and use it in GitHub Desktop.
Save chicago-joe/5e1ba42d4013a4d6b0639b2b7c12eab2 to your computer and use it in GitHub Desktop.
searching through strings using pandas dataframes
import pandas as pd
df = pd.DataFrame(
{
'ticker':
[
'AAPL',
'SPY',
'A',
'GP',
'NFLX',
'SPY',
'FB',
'CPI',
'SNAP'
],
'allowed_tickers':
[
'BA;AAPL;TLT',
'SPY;GS',
'AAA;AA;AAPL',
'GS;GP',
'TLT;SPY;GS;NFLX;AAA;FB',
'BA;AAPL;TLT;QQQ;SPY',
'SPY;GS;NFLX;AAA;SNAP',
'AAPL;TLT;SPY;GS;NFLX',
'SPY;GS;NFLX;SNAP'
]
}
)
check = ['Allowed' if (";" + df['ticker'][i] + ";") in df['allowed_tickers'][i] or (
(df['ticker'][i] + ";") in df['allowed_tickers'][i] and df['allowed_tickers'][i].find((df['ticker'][i] + ";")) == 0) or (
(";" + df['ticker'][i]) in df['allowed_tickers'][i] and df['allowed_tickers'][i].find(
(";" + df['ticker'][i])) == len(df['allowed_tickers'][i]) - len((";" + df['ticker'][i]))) or len(
df['allowed_tickers'][i]) == len((df['ticker'][i])) else '***INVALID TICKER***' for i in range(len(df['ticker']))]
df['validation'] = check
df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment