0

I want to search data from the first df into the second df, So I want to search the "name" or "category" column value from first df into second df column "headline" and "category" if the value match in any of that column then take the column value and add a new column in first df database.

I tried using for loop but takes so much because the data is so big and think this is not the best solution. so can someone help me to find the value and store it?

data = []
for index, row in df2.iterrows():
    for i, r in df1.iterrows():
        # check row name is i headline or category is r category
        if row['name'] in r['headline'] or row['name'] in r['category']:
            data.append({
                "name": row['name'],
                "category": row['category'],
                # Get matching category from df1\
                "find": r['category'] if row['name'] in r['category'] else r['headline']
            })
        elif row['category'] in r['headline'] or row['category'] in r['category']:
            data.append({
                "name": row['name'],
                "category": row['category'],
                # Get matching category from df1\

                "find": r['category'] if row['category'] in r['category'] else r['headline']
            })
data
    

First Df data

name    category    
0       Agriculture farming 
1       Agriculture agribusiness    
2       Agriculture husbandry   
3       Agriculture agriculture department  
4       Agriculture department of agriculture   

Second Df DATA

    headline                                            category
0   Over 4 Million Americans Roll Up Sleeves For O...   U.S. NEWS
1   American Airlines Flyer Charged, Banned For Li...   U.S. NEWS
2   23 Of The Funniest Tweets About Cats And Dogs ...   COMEDY
3   The Funniest Tweets From Parents This Week (Se...   PARENTING
4   Woman Who Called Cops On Black Bird-Watcher Lo...   U.S. NEWS
... ... ...
95  Fast-Moving Fairview Fire Kills At Least 2 In ...   U.S. NEWS
96  Kody Clemens Strikes Out MVP Shohei Ohtani, Tr...   SPORTS
97  Mississippi Governor Says Water Pressure Is No...   U.S. NEWS
98  Meta, Parent Company Of Instagram, Fined $400 ...   U.S. NEWS
99  School Starts Today For Survivors Of Deadly Ma...   U.S. NEWS
rahul raj
  • 21
  • 1
  • 8

0 Answers0