Combine two Pandas series into a DataFrame - GeeksforGeeks

28 Jul, 2020
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 libraryimport pandas as pd  # this user defines function# creates a series# from the passed list.def createSeries (series_list):      # create a series  series_list = pd.Series(series_list)      return series_list  # create a series of studentsstudents = createSeries(['ABC', 'DEF',                         'GHI', 'JKL',                         'MNO', 'PQR'])  # create a series of subjectssubject = createSeries(['C++', 'C#',                         'RUBY', 'SWIFT',                        'GO', 'PYTHON'])# create a series of marksmarks = createSeries([90, 30,                       50, 70,                       80, 60])# create a dictonarydata = {"students": students,        "subject": subject,        "marks": marks}  # Concatenating the series side# by side as depicted by axis=1# If you want to concatenate the # series one below the other# change the axis to zero.df = pd.concat(data,               axis = 1)  # show the dataframedf
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 libraryimport pandas as pd  # create a seriesa = pd.Series(["ABC", "DEF",                "GHI"])  # create a seriesb = pd.Series(["JKL", "MNO",                "PQR"])  # combine two series then# create a dataframedf = pd.DataFrame(a.append(b,                   ignore_index = True))# show the dataframedf
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 libraryimport pandas as pd  # create a seriesa = pd.Series(["C++", "JAVA",                "PYTHON", "DBMS",               "C#"], name = "subjects")  # create a seriesb = pd.Series(["30", "60",                "90", "56",                "50"], name = "marks")  # merge both series df = pd.merge(a, b, right_index = True,               left_index = True)# show the dataframedf
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 libraryimport pandas as pd  # create a seriesa = pd.Series(["C++", "JAVA",                "PYTHON", "DBMS",                "C#"], name = "subjects")  # create a seriesb = pd.Series(["30", "60",                "90", "56",                "50"], name = "marks")  # create a dataframea = pd.DataFrame(a)  # add series 'b' # into dataframe 'a'df = a.join(b)  # show the dataframedf
Output: 
Python pandas-dataFrame
Python pandas-series
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Create a Pandas DataFrame from Lists
*args and **kwargs in Python
How to drop one or multiple columns in Pandas Dataframe
sum() function in Python
How To Convert Python Dictionary To JSON?
Convert integer to string in Python