Write a Python code to combine two given series and convert it to a dataframe

When working with Pandas Series, you often need to combine them into a single DataFrame for analysis. Python provides several methods to achieve this: direct DataFrame creation, concatenation, and joining.

Method 1: Using DataFrame Constructor

Create a DataFrame from the first series, then add the second series as a new column ?

import pandas as pd

series1 = pd.Series([1, 2, 3, 4, 5], name='Id')
series2 = pd.Series([12, 13, 12, 14, 15], name='Age')

df = pd.DataFrame(series1)
df['Age'] = series2
print(df)
   Id  Age
0   1   12
1   2   13
2   3   12
3   4   14
4   5   15

Method 2: Using pd.concat()

Concatenate both series horizontally using axis=1 parameter ?

import pandas as pd

series1 = pd.Series([1, 2, 3, 4, 5], name='Id')
series2 = pd.Series([12, 13, 12, 14, 15], name='Age')

df = pd.concat([series1, series2], axis=1)
print(df)
   Id  Age
0   1   12
1   2   13
2   3   12
3   4   14
4   5   15

Method 3: Using join()

Create a DataFrame from the first series and join the second series ?

import pandas as pd

series1 = pd.Series([1, 2, 3, 4, 5], name='Id')
series2 = pd.Series([12, 13, 12, 14, 15], name='Age')

df = pd.DataFrame(series1)
df = df.join(series2)
print(df)
   Id  Age
0   1   12
1   2   13
2   3   12
3   4   14
4   5   15

Comparison

Method Best For Performance
DataFrame Constructor Simple two-series combination Good
pd.concat() Multiple series or complex operations Best
join() Adding series to existing DataFrame Good

Conclusion

Use pd.concat() for combining multiple series efficiently. Use DataFrame constructor for simple two-series combinations. The join() method works well when adding series to existing DataFrames.

Updated on: 2026-03-25T16:35:26+05:30

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements