In this post, we will learn how to combine two series into a DataFrame? Before starting let’s see what a series is?
Pandas Series is a one-dimensional labeled array capable of holding any data type. In other terms, Pandas Series is nothing but a column in an excel sheet.
There are several ways to concatenate two series in pandas. Following are some of the ways:
Method 1: Using pandas.concat().
This method does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.
Code:
python
import pandas as pd
def createSeries (series_list):
series_list = pd.Series(series_list)
return series_list
students = createSeries([ 'ABC' , 'DEF' ,
'GHI' , 'JKL' ,
'MNO' , 'PQR' ])
subject = createSeries([ 'C++' , 'C#' ,
'RUBY' , 'SWIFT' ,
'GO' , 'PYTHON' ])
marks = createSeries([ 90 , 30 ,
50 , 70 ,
80 , 60 ])
data = { "students" : students,
"subject" : subject,
"marks" : marks}
df = pd.concat(data,
axis = 1 )
df
|
Output:

Method 2: Using Series.append().
This method is a shortcut to concat. This method concatenates along axis=0 i.e. rows. Series.append() can take multiple objects to concatenate.
Code:
Python3
import pandas as pd
a = pd.Series([ "ABC" , "DEF" ,
"GHI" ])
b = pd.Series([ "JKL" , "MNO" ,
"PQR" ])
df = pd.DataFrame(a.append(b,
ignore_index = True ))
df
|
Output:

Method 3: Using pandas.merge().
Pandas have high performance in-memory join operations which is very similar to RDBMS like SQL. merge can be used for all database join operations between dataframe or named series objects. You have to pass an extra parameter “name” to the series in this case.
Code:
Python3
import pandas as pd
a = pd.Series([ "C++" , "JAVA" ,
"PYTHON" , "DBMS" ,
"C#" ], name = "subjects" )
b = pd.Series([ "30" , "60" ,
"90" , "56" ,
"50" ], name = "marks" )
df = pd.merge(a, b, right_index = True ,
left_index = True )
df
|
Output:

Method 4: Using Dataframe.join().
This method can be used also to join two series but you have to convert one series into dataframe.
Code:
Python3
import pandas as pd
a = pd.Series([ "C++" , "JAVA" ,
"PYTHON" , "DBMS" ,
"C#" ], name = "subjects" )
b = pd.Series([ "30" , "60" ,
"90" , "56" ,
"50" ], name = "marks" )
a = pd.DataFrame(a)
df = a.join(b)
df
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!