Given a dataframe of text type, I want to create a new column consisting on the concatenation of all the columns for a specific row (just like in the picture) .
The dataframe of this example is given by:
df= pd.DataFrame({'A':['hello word','how are u doing'],'B':['hey!','im doing great'],'C':['lol','lmao']})
This can be done with
df['Joined] = df['A']+df['B']+df['C']
, but I want this to work for different number of columns. I solved the problem in two different ways:
1.
df['Joined']=''
for col in du.columns:
df['Joined']+= ' '+ df[col]
- l = list() for index, row in du.iterrows(): l.append(' '.join([x for x in row])) du['Joined'] = l
I would like to know if there is a more elegant way to do this (and more efficient)