2

Combine two Series into a DataFrame in Pandas

 1 year ago
source link: https://thispointer.com/combine-two-series-into-a-dataframe-in-pandas/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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)
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
2 Australia
3 Italy
Name: Country, dtype: object
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.

# Series 2
city = pd.Series(['New Delhi', 'New York', 'Canberra', 'Paris', 'Abu Dhabi'], name = "City")
print (city)
# 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
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)
# 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
     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)
# 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
     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)
# 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
     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)
# 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
     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)
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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK