0

I'm trying to make a list based on a data frame, where if a string is found under the "question" column, it is added. I seem to have made it work with a singular string, but I am not sure how to apply this to a list.

#pd.set_option("display.max_rows", None, "display.max_columns", None)
pd.set_option('display.max_colwidth', -1)
jp = pd.read_csv('jeopardy.csv', delimiter = ",")


jp = jp.rename(columns = {'Show Number': 'show_number', ' Air Date': 'air_date', ' Round': 'round', ' Category': 'category' , " Value": 'value', ' Question': 'question', ' Answer': 'answer'})
#print(jp.head())
print(jp.info())

jp_df = jp[jp.apply(lambda row: 'King' in row['question'], axis = 1)].reset_index(drop=True)
print(jp_df.info())
  • @cs95 I tried jp_df = jp[jp['question'].isin(c1)] c1 = ['King', 'Emperor'] which is what that link recommends. Got an empty data frame – Samuel Patterson Jul 14 '20 at 18:39
  • 1
    Oh oh, apologies, you wanted a partial string match? Take a look at `df2 = df[df['question'].str.contains('|'.join(c1))]` [More info on partial string match.](https://stackoverflow.com/a/55335207/4909087) – cs95 Jul 14 '20 at 18:41
  • @cs95 Yes! Thank You! – Samuel Patterson Jul 14 '20 at 18:48
  • 1
    No problem, please consider upvoting my content [here](https://stackoverflow.com/a/55335207/4909087) if you found it helpful. Ty. – cs95 Jul 14 '20 at 18:50

1 Answers1

0

I think this is what you want:

pd.set_option("display.max_rows", None, "display.max_columns", None)
pd.set_option('display.max_colwidth', -1)
jp = pd.read_csv('jeopardy.csv', delimiter = ",")


jp = jp.rename(columns = {'Show Number': 'show_number', ' Air Date': 'air_date', ' Round': 'round', ' Category': 'category' , " Value": 'value', ' Question': 'question', ' Answer': 'answer'})

values_wanted = ['King', ' Queen']

jp_list = jp[jp['question'].isin(values_wanted)]
  • I shouldn't have called it a list, I am trying to make a data frame. Also I am trying to check for more then just "King". I want to input a list. – Samuel Patterson Jul 14 '20 at 18:38
  • @SamuelPatterson Okay, edited to reflect what you need –  Jul 14 '20 at 18:42