4

How can I concatenate two Series and create one DataFrame ? For example, I have series like:

a=pd.Series([1,2,3])
b=pd.Series([4,5,6])

And, I want to get a data frame like:

pd.DataFrame([[1,4], [2,5], [3,6]])

3 Answers 3

4

Shortest would be:

pd.DataFrame([a,b]).T

Or:

pd.DataFrame(zip(a,b))

   0  1
0  1  4
1  2  5
2  3  6
Sign up to request clarification or add additional context in comments.

Comments

3

Or use concat:

>>> pd.concat([a,b],axis=1)
   0  1
0  1  4
1  2  5
2  3  6
>>> 

Or join:

>>> a.to_frame().join(b.to_frame(name=1))
   0  1
0  1  4
1  2  5
2  3  6
>>> 

Comments

1

Another possible faster solution could be,

pd.DataFrame(np.vstack((a,b)).T)

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.