0

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?

Incognito
  • 331
  • 1
  • 5
  • 14

3 Answers3

3

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
Nuri Taş
  • 3,828
  • 2
  • 4
  • 22
2

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
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
2

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
Rabinzel
  • 7,757
  • 3
  • 10
  • 30