How can I concatenate a series on a DataFrame using Pandas?

If I have a DataFrame :

 students = pd.DataFrame([ ['Alex'], ['Lauren'], ]) 

How can I combine a Series and create a new DataFrame ? For example, I would like to:

 >>> marks = pd.Series([.8, .75]) >>> students.concat(marks).values [['Alex', .8], ['Lauren', .75]] 

I know I can use:

 students['marks'] = marks 

But this will mutate the students .

I tried:

  >>> pd.concat([students, marks]) … AttributeError: 'Series' object has no attribute '_data' 
+6
source share
3 answers

Now you can convert to a DataFrame and concatenate:

 >>> pd.concat([students, pd.DataFrame(marks)], axis=1) 0 0 0 Alex 0.80 1 Lauren 0.75 
+6
source

To save the original data frame, you can first copy the data and then add a column:

 students2 = students.copy(deep=True) students2['marks'] = marks 
0
source

In this case, I usually did

students["marks"] = marks

But pd.concat works.

0
source

All Articles