jumpman23
jumpman23

Reputation: 395

Python Loop for Multiple Row String Concatination

I am looking to create a loop in python that will concatenate multiple rows of strings together. I have created the table that I have now listed as "Before" and then the table I am trying to create "After". Any thoughts on how to do this? I am currently using the following code to get just one string but I need to be able to loop the entire data frame:

df.str.cat(sep='')

Before:

Text       |    Channel  |  Destination   | Amount  | Total
string1           NaN           NaN           NaN      NaN
string2           DKI           US             34       5   
string3           NaN           NaN           NaN      NaN
string4           DKI           CA             39       20   

After:

Text           |    Channel  |  Destination   | Amount  | Total
string1string2        DKI           US            34       5
string3string4        DKI           CA            39       20   

Upvotes: 0

Views: 421

Answers (1)

jezrael
jezrael

Reputation: 862611

Create helper Series by shift, check non NaNs by notna and create groups by cumsum.

Then aggregate dy dict of functions, remove index name and for same columns order add reindex:

a = df['total'].shift().notna().cumsum()
#for oldier pandas versions
#a = df['total'].shift().notnull().cumsum()
d = {'row':'first', 'total':'last', 'Text':''.join}

df = df.groupby(a).agg(d).rename_axis(None).reindex(columns=df.columns)
print (df)
   row            Text  total
0    1  string1string2    3.0
1    3  string3string4    1.0

Upvotes: 2

Related Questions