Skip to content
Related Articles

Related Articles

Creating a dataframe from Pandas series

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 25 Jul, 2022
Improve Article
Save Article

Series is a type of list in pandas which can take integer values, string values, double values and more. But in Pandas Series we return an object in the form of list, having index starting from 0 to n, Where n is the length of values in series. Later in this article, we will discuss dataframes in pandas, but we first need to understand the main difference between Series and Dataframe. Series can only contain single list with index, whereas dataframe can be made of more than one series or we can say that a dataframe is a collection of series that can be used to analyse the data. 

Code #1: Creating a simple Series 

Python3




#importing pandas library
import pandas as pd
 
#Creating a list
author = ['Jitender', 'Purnima', 'Arpit', 'Jyoti']
#Creating a Series by passing list variable to Series() function
auth_series = pd.Series(author)
#Printing Series
print(auth_series)

Output:

0    Jitender
1     Purnima
2       Arpit
3       Jyoti
dtype: object

Let’s check type of Series: 

Python3




print(type(auth_series))

Output:

<class 'pandas.core.series.Series'>

  Code #2: Creating Dataframe from multiple Series 

Python3




#Importing Pandas library
import pandas as pd
 
#Creating two lists
author = ['Jitender', 'Purnima', 'Arpit', 'Jyoti']
article = [210, 211, 114, 178]
#Creating two Series by passing lists
auth_series = pd.Series(author)
article_series = pd.Series(article)
#Creating a dictionary by passing Series objects as values
frame = { 'Author': auth_series, 'Article': article_series }
#Creating DataFrame by passing Dictionary
result = pd.DataFrame(frame)
#Printing elements of Dataframe
print(result)

Output:

     Author  Article
0  Jitender      210
1   Purnima      211
2     Arpit      114
3     Jyoti      178

Explanation: We have created two lists ‘author’ and article’ which have been passed to Series() functions to create two Series. After creating Series, we have created a dictionary and passed Series objects as values of the dictionary and keys of the dictionary will be served as Columns of the dataframe.

   

Code #3: How to add new column to the dataframe

Python3




#Importing pandas library
import pandas as pd
#Creating Series
auth_series = pd.Series(['Jitender', 'Purnima', 'Arpit', 'Jyoti'])
article_series = pd.Series([210, 211, 114, 178])
#Creating Dictionary
frame = { 'Author': auth_series, 'Article': article_series }
#Creating Dataframe
result = pd.DataFrame(frame)
#Creating another list
age = [21, 21, 24, 23]
##Creating new column in the dataframe by providing s Series created using list
result['Age'] = pd.Series(age)
#Printing dataframe
print(result)

Output:

     Author  Article  Age
0  Jitender      210   21
1   Purnima      211   21
2     Arpit      114   24
3     Jyoti      178   23

Explanation: We have added one more series externally named as age of the authors, then directly added this series in the pandas dataframe. Remember one thing if any value is missing then by default it will be converted into NaN value, i.e, null by default.   

Code #4: Missing value in dataframe 

Python3




#Importing pandas library
import pandas as pd
#Creating Series
auth_series = pd.Series(['Jitender', 'Purnima', 'Arpit', 'Jyoti'])
article_series = pd.Series([210, 211, 114, 178])
#Creating Dictionary
frame = { 'Author': auth_series, 'Article': article_series }
#Creating Dataframe
result = pd.DataFrame(frame)
#Creating another list
age = [21, 21, 24]
##Creating new column in the dataframe by providing s Series created using list
result['Age'] = pd.Series(age)
#Printing dataframe
print(result)

Output:

     Author  Article   Age
0  Jitender      210  21.0
1   Purnima      211  21.0
2     Arpit      114  23.0
3     Jyoti      178   NaN

Code#5: Creating a Dataframe using dictionary of Series 

  

Python3




#Importing pandas library
import pandas as pd
#Creating dictionary of Series
dict1={'Auth_Name':pd.Series(['Jitender', 'Purnima', 'Arpit', 'Jyoti']),
       'Author_Book_No': pd.Series([210, 211, 114, 178]),
         'Age': pd.Series([21, 21, 24, 23]) }
  
#Creating Dataframe
df = pd.DataFrame(dict1)
#Printing dataframe
print(df)

Output:

   Auth_Name  Auth_Book_No  Age
0  Jitender           210   21
1   Purnima           211   21
2     Arpit           114   24
3     Jyoti           178   23

Explanation: Here, we have passed a dictionary which has been created using a series as values then passed this dictionary to create a dataframe. We can see while creating a dataframe using dictionary, the keys of the dictionary will become Columns and values will become Rows.

Code#6: Adding explicit index to dataframe 

Python3




#Importing pandas library
import pandas as pd
#Creating dictionary of Series
dict1={'Auth_Name':pd.Series(['Jitender', 'Purnima', 'Arpit', 'Jyoti']),
       'Author_Book_No': pd.Series([210, 211, 114, 178]),
         'Age': pd.Series([21, 21, 24, 23]) }
  
#Creating Dataframe
df = pd.DataFrame(dict1,index=['SNo1','SNo2','SNo3','SNo4'])
#Printing dataframe
print(df)

Output:

          Auth_Name     Author_Book_No  Age
SNo1       NaN             NaN          NaN
SNo2       NaN             NaN          NaN
SNo3       NaN             NaN          NaN
SNo4       NaN             NaN          NaN

Explanation: Here we can see after providing index to the dataframe explicitly, it has fill all data with NaN values since we have created this dataframe using Series and Series has its own default indices(0,1,2) that is why when indices of both dataframe and Series do not match, we got all NaN values. We can rectify this problem by providing same index values to every Series elements. Let see how can we do this.

Python3




#This code is provided by Sheetal Verma
#Importing pandas library
import pandas as pd
#Creating dictionary of Series
dict1={'Auth_Name':pd.Series(['Jitender', 'Purnima', 'Arpit', 'Jyoti'],index=['SNo1','SNo2','SNo3','SNo4']),
       'Author_Book_No': pd.Series([210, 211, 114, 178],index=['SNo1','SNo2','SNo3','SNo4']),
         'Age': pd.Series([21, 21, 24, 23],index=['SNo1','SNo2','SNo3','SNo4']) }
  
#Creating Dataframe
df = pd.DataFrame(dict1,index=['SNo1','SNo2','SNo3','SNo4'])
#Printing dataframe
print(df)

Output:

      Auth_Name       Author_Book_No  Age
SNo1  Jitender             210        21
SNo2   Purnima             211        21
SNo3     Arpit             114        24
SNo4     Jyoti             178        23

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!