2021年4月6日星期二

Combine 2 columns which are having comma separated strings into 1 column in pandas

I have a dataframe

df = pd.DataFrame([["A","a$b,c$d,k$m","h,y,a"], ["B","n$e,d$w,t$y","t,r,s"]], columns=["id","c1","c2"])  

I want to combine each element of column c1 which are separated by a comma with another element of column c2 with an asterisk(*)

Expected output:

df_out = pd.DataFrame([["A","a$b*h,c$d*y,k$m*a"], ["B","n$e*t,d$w*r,t$y*s"]], columns=["id","c3"])  

How to do it?

https://stackoverflow.com/questions/66979298/combine-2-columns-which-are-having-comma-separated-strings-into-1-column-in-pand April 07, 2021 at 11:45AM

没有评论:

发表评论