0

I'm new to pandas, and I'm trying to create a data frame by pulling partial strings from one of the columns of the original

recipe_info = {
    'title' = [ 'waffles', 'eggs', 'chocolate pancakes', 'pancakes']
    'rating' = [ 3.4, 2.8, 3,6, 1.5 ]

my_recipes = pd.DataFrame(recipe_info)

recipe_info[recipe_info['title'].str.contains("pancake")]

desired output:

all_the_pancakes = {
     'title' = ['chocolate pancakes', 'pancakes']
     'rating' = [3.6, 1.5]

Thanks in advance...

Sociopath
  • 13,068
  • 19
  • 47
  • 75

2 Answers2

0

You need:

recipe_info = {
    'title': [ 'waffles', 'eggs', 'chocolate pancakes', 'pancakes'],
    'rating': [ 3.4, 2.8, 3.6, 1.5 ]}


my_recipes = pd.DataFrame(recipe_info)

new_df = my_recipes[my_recipes['title'].str.contains('pancakes')]
print(new_df)

Output:

   rating               title                                                                                                         
2     3.6  chocolate pancakes                                                                                                         
3     1.5            pancakes   
Sociopath
  • 13,068
  • 19
  • 47
  • 75
0

Use can also use filter():

new_df =df.set_index('title').filter(like='pancakes', axis = 0).reset_index()
Loochie
  • 2,414
  • 13
  • 20