
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Write a Python code to combine two given series and convert it to a dataframe
Assume, you have two series and the result for combining two series into dataframe as,
Id Age 0 1 12 1 2 13 2 3 12 3 4 14 4 5 15
To solve this, we can have three different approaches.
Solution 1
Define two series as series1 and series2
Assign first series into dataframe. Store it as df
df = pd.DataFrame(series1)
Create a column df[‘Age’] in dataframe and assign second series inside to df.
df['Age'] = pd.DataFrame(series2)
Example
Let’s check the following code to get a better understanding −
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'] = pd.DataFrame(series2) print(df)
Output
Id Age 0 1 12 1 2 13 2 3 12 3 4 14 4 5 15
Solution 2
Define a two series
Apply pandas concat function inside two series and set axis as 1. It is defined below,
pd.concat([series1,series2],axis=1)
Example
Let’s check the following code to get a better understanding −
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)
Output
Id Age 0 1 12 1 2 13 2 3 12 3 4 14 4 5 15
Solution 3
Define a two series
Assign first series into dataframe. Store it as df
df = pd.DataFrame(series1)
Apply pandas join function inside series2. It is defined below,
df = df.join(series2) pd.concat([series1,series2],axis=1)
Example
Let’s check the following code to get a better understanding −
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)
Output
Id Age 0 1 12 1 2 13 2 3 12 3 4 14 4 5 15
- Related Articles
- Write a Python code to swap last two rows in a given dataframe
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- Write a Python code to rename the given axis in a dataframe
- Write a Python code to filter palindrome names in a given dataframe
- Write a Python program to separate a series of alphabets and digits and convert them to a dataframe
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a Python code to select any one random row from a given DataFrame
- Write a Python code to fill all the missing values in a given dataframe
- Write a Python code to convert a given series into a dummy variable and drop any NaN values if they exist
- Write a Python code to concatenate two Pandas series into a single series without repeating the index
- Write a Python code to find the second lowest value in each column in a given dataframe
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to transpose the index and columns in a given DataFrame
- Program to convert gray code for a given number in python
- Write a program in Python to localize Asian timezone for a given dataframe

Advertisements