Combine two Series into a DataFrame in Pandas

In this article, we will discuss various ways to combine two Series into a Pandas DataFrame.

Table of Contents

Preparing DataSet

To quickly get started, let’s create two pandas Series to experiment. We’ll use the pandas library with some random data.

import pandas as pd

# Series 1
country = pd.Series(['India', 'US', 'Australia', 'Italy', 'UAE'],  name = "Country")

print (country)

Contents of the created Series are,

0        India
1           US
2    Australia
3        Italy
4          UAE
Name: Country, dtype: object

Creating another Series that can be concatenated with the first Series into DataFrame.

Advertisements
# Series 2
city = pd.Series(['New Delhi', 'New York', 'Canberra', 'Paris', 'Abu Dhabi'],  name = "City")

print (city)
0    New Delhi
1     New York
2     Canberra
3        Paris
4    Abu Dhabi
Name: City, dtype: object

Method 1: Using pandas.concat() function

The simplest way to combine two Series into pandas DataFrame is by using the pandas.concat() function which combines both the series and return DataFrame directly. Let’s concat the two Series that we have created above.

# concat the series
df = pd.concat([country, city], axis=1)

print (df)

Output

     Country       City
0      India  New Delhi
1         US   New York
2  Australia   Canberra
3      Italy      Paris
4        UAE  Abu Dhabi

As observed, it has concatenated both the pandas Series into a DataFrame. The Series names have been assigned as the column names in the resulting DataFrame.

Method 2: Using join() function

Another method is to use the join() function, here, we will first convert one of the series to DataFrame using the “to_frame” function and then use the join to combine them into a pandas DataFrame. Let’s look at the code below.

# join the series
df = country.to_frame().join(city)

print(df)

Output

     Country       City
0      India  New Delhi
1         US   New York
2  Australia   Canberra
3      Italy      Paris
4        UAE  Abu Dhabi

Method 3: Using merge() function

The pandas.merge() function is commonly used to merge two DataFrames, however, it can also be used to combine two Series into DataFrame as below.

# merge the series
df = pd.merge(country, city, right_index=True, left_index=True)

print(df)

Output

     Country       City
0      India  New Delhi
1         US   New York
2  Australia   Canberra
3      Italy      Paris
4        UAE  Abu Dhabi

Method 4: Using pandas dictionary

An alternate method is to first combine the two Series using a dictionary and later convert them into DataFrame using pandas.DataFrame. Let’s understand by looking at the code below.

# use dictionary
df = pd.DataFrame(dict(Country = country, City = city))

print(df)

Output

     Country       City
0      India  New Delhi
1         US   New York
2  Australia   Canberra
3      Italy      Paris
4        UAE  Abu Dhabi

As observed, first we created the dictionary with keys “Country” and “City” and values containing the individual Series. Post that, we converted the dictionary to DataFrame to get a similar output.

The Complete example is as follow,

import pandas as pd

# Series 1
country = pd.Series(['India', 'US', 'Australia', 'Italy', 'UAE'],  name = "Country")

# Series 2
city = pd.Series(['New Delhi', 'New York', 'Canberra', 'Paris', 'Abu Dhabi'],  name = "City")

# concat the series
df = pd.concat([country, city], axis=1)

print (df)

# join the series
df = country.to_frame().join(city)

print(df)

# merge the series
df = pd.merge(country, city, right_index=True, left_index=True)

print(df)

# use dictionary
df = pd.DataFrame(dict(Country = country, City = city))

print(df)

Summary

In this article, we have discussed how to combine two Series into a DataFrame in pandas. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top