How to explode Python Pandas Dataframe and merge strings from other dataframe?

2024/7/8 8:23:32

Dataframe1 has a lot of rows and columns of data. One column is Text. Certain rows in Text column have strings and some strings include within the strings this {ExplodeEList2}

How to explode (expand) those specific rows of Dataframe1 and replace {ExplodeEList2} in each string with each name contained in the separate dataframe EList2['Name']? Thank you! I've been banging my head against my keyboard all day trying to solve this.

Dataframe1:

Text
Unrelated data
Random sample text {ExplodeElist2} and more random sample text.
Other unrelated data

EList2:

Name
Jack
Jon
Sally

How do I generate this in Dataframe1:

Text
Unrelated data
Random sample text Jack and more random sample text.
Random sample text Jon and more random sample text.
Random sample text Sally and more random sample text.
Other unrelated data
Answer

You can use apply to process all the Text values in DataFrame1 which contain the string ExplodeElist2, replacing the string with a list of replaced values. You can then explode that list:

mask = DataFrame1['Text'].str.contains('{ExplodeElist2}')
DataFrame1.loc[mask, 'Text'] = DataFrame1.loc[mask, 'Text'].apply(lambda s:[s.replace('{ExplodeElist2}', n) for n in Elist2['Name']])
DataFrame1 = DataFrame1.explode('Text').reset_index(drop=True)

Output (for your sample data):

                                                Text
0                                     Unrelated data
1  Random sample text Jack and more random sample...
2  Random sample text Jon and more random sample ...
3  Random sample text Sally and more random sampl...
4                               Other unrelated data
https://en.xdnf.cn/q/120130.html

Related Q&A

Creating a new column with numbers in Pandas to group with a column with existing numbers

Good day, I have a column from a data frame here:A231011 22My objective is to create a new column and associate the numbers like this:A file_number 23 8 10 6 11 6 22 8As…

How to get PDF file from the binary data of SoftLayers quote?

I got the binary data by "getPdf" method of SoftLayers API.Ref. BillingSoftLayer_Billing_Order_Quote::getPdf | SoftLayer Development Network - http://sldn.softlayer.com/reference/services/Sof…

How to list of elements and use those elements as a header of pandas dataframe?

I have a list with some elements. For example: list= [name, phone_number,age,gender] I want to use these elements as a header or column name in a pandas dataframe. I would really appreciate your ideas.…

Averaging Filter using python

I am new in python and trying apply averaging filter on image as the way i understand averaging concept summing up the neighboring elements including itself and divide it by number of elementstechnique…

Click on Show more deals in webpage with Selenium

Id like to click on every Show 10 more deals on the following page: "https://www.uswitch.com/broadband/compare/deals_and_offers/" but it does not seem to work. Im stuck having the following e…

In Matplotlib, what axis attribute specifies the spacing between ticks? [duplicate]

This question already has answers here:Changing the tick frequency on the x or y axis(15 answers)Closed 6 years ago.When generating a Matplotlib line or scatter plot, what axis attribute specifies the …

How can I make a simple calculator in python 3?

Im making this calculator using python 3, and this is what I have so far:print("Welcome to Calculator!")class Calculator:def addition(self,x,y):added = x + yreturn addeddef subtraction(self,x…

python 3: lists dont change their values

So I am trying to change a bunch of list items by a random percentage using a for loop.import random as rdm list = [1000, 100, 50, 25] def change():for item in list:item = item + item*rdm.uniform(-10, …

Using zip_longest on unequal lists but repeat the last entry instead of returning None

There is an existing thread about this Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped But its not quite Im after. Instead of returning None, …

python scrapy not crawling all urls in scraped list

I am trying to scrape information from the pages listed on this page. https://pardo.ch/pardo/program/archive/2017/catalog-films.htmlthe xpath selector:film_page_urls_startpage = sel.xpath(//article[@cl…