Home > Blockchain >  Concatenate strings in dataframe rows (Python - pandas)
Concatenate strings in dataframe rows (Python - pandas)

Time:08-27

Let's say I have the following dataframe d1:

d1 = pd.DataFrame(data = {'col1': ["A", "C"], 'col2': ["B", "D"]})

enter image description here

I want to built a dataframe d2 with a single row. That row would concatenate the values of d1's rows, separated by a space. That is:

d2 = pd.DataFrame(data = {'col1': ["A B"], 'col2': ["C D"]})

enter image description here

What should I do?

CodePudding user response:

You can use pandas.apply with axis=1 for iterating over rows then rename columns like 0 -> col1 & 1 -> col2.

d1 = pd.DataFrame(data = {'col1': ["A", "C"], 'col2': ["B", "D"]})
res = pd.DataFrame(d1.apply(lambda row: ' '.join(row), axis=1)).T.rename(columns = lambda x: f'col{x 1}')
print(res)

  col1 col2
0  A B  C D

CodePudding user response:

Join the rows together with pd.apply with axis=1, convert the result to list and pass it to a new dataframe. The columns you can take from d1.

d2 = pd.DataFrame([d1.apply(" ".join, axis=1).tolist()], columns=d1.columns)
print(d2)
  col1 col2
0  A B  C D

CodePudding user response:

You can also use agg to operate join. However, it will return a pd.Series, so convert it to a dataframe with pd.Series.to_frame:

d2 = d1.agg(' '.join).to_frame().T
  • Related