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?

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.