The Wayback Machine - https://web.archive.org/web/20240902213721/https://www.geeksforgeeks.org/creating-a-dataframe-from-pandas-series/
Open In App

Creating a dataframe from Pandas series

Last Updated : 29 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Series is a type of list in Pandas that can take integer values, string values, double values, and more. But in Pandas Series we return an object in the form of a list, having an index starting from 0 to n, Where n is the length of values in the 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 a single list with an 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 analyze the data.

Creating Pandas DataFrames from 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 the type of Series: 

Python3




print(type(auth_series))


Output:

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

Create DataFrame From Multiple Series

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

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

Add a Column in Pandas Dataframe

We have added one more series externally named as the age of the authors, then directly added this series in the Pandas 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

Missing value in 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.

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

Creating a Dataframe using a dictionary of Series

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

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

Explicit Indexing in Pandas Dataframe

Here we can see after providing an index to the dataframe explicitly, it has filled all data with NaN values since we have created this dataframe using Series and Series has its own default indices(0,1,2) which is why when indices of both dataframe and Series do not match, we got all NaN values. 

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

Here, we can rectify this problem by providing the same index values to every Series element. 

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


Previous Article
Next Article

Similar Reads

Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
Convert a series of date strings to a time series in Pandas Dataframe
During the analysis of a dataset, oftentimes it happens that the dates are not represented in proper type and are rather present as simple strings which makes it difficult to process them and perform standard date-time operations on them. pandas.to_datetime() Function helps in converting a date string to a python date object. So, it can be utilized
3 min read
Pandas Series dt.weekofyear Method | Get Week of Year in Pandas Series
The dt.weekofyear attribute returns a Series containing the week ordinal of the year in the underlying data of the given series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index =
2 min read
Pandas Series dt.minute | Extract Minute from DateTime Series in Pandas
Pandas Series.dt.minute attribute returns a NumPy array containing the minutes of the DateTime in the underlying data of the given series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
2 min read
Pandas Series dt.dayofweek | Get Day of Week from DateTime Series in Pandas
Pandas dt.dayofweek attribute returns the day of the week from the given DateTime Series Object. It is assumed the week starts on Monday, which is denoted by 0, and ends on Sunday which is denoted by 6. Example: C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:
2 min read
Pandas Series dt.freq | Retrieve Frequency of Pandas Time Series
Pandas dt.freq attribute returns the time series frequency applied on the given series object if any, else it returns None. Examples C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_da
2 min read
Pandas Series dt.daysinmonth | Get Number of Days in Month in Pandas Series
The dt.daysinmonth attribute returns the number of days in the month for the given DateTime series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime(sr) result
2 min read
Pandas Series dt.normalize() | Normalize Time in Pandas Series
The dt.normalize() method converts times to midnight. The time component of the date-time is converted to midnight i.e. 00:00:00. This is useful in cases when the time does not matter. Length is unaltered. The time zones are unaffected. Example: C/C++ Code import pandas as pd sr = pd.Series(pd.date_range('2012-12-31 09:45', periods = 5, freq = 'M',
2 min read
Creating views on Pandas DataFrame
Many times while doing data analysis we are dealing with a large data set, having a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that purpose, let's see how we can create views on the Dataframe and select only those columns that we need and
2 min read
Creating views on Pandas DataFrame | Set - 2
Prerequisite: Creating views on Pandas DataFrame | Set - 1 Many times while doing data analysis we are dealing with a large data set has a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that purpose, let's see how we can create views on the D
2 min read
Creating Pandas dataframe using list of lists
In this article, we will explore the Creating Pandas data frame using a list of lists. A Pandas DataFrame is a versatile 2-dimensional labeled data structure with columns that can contain different data types. It is widely utilized as one of the most common objects in the Pandas library. There are various methods for Creating a Pandas data frame us
4 min read
Creating a Pandas dataframe using list of tuples
Pandas is famous for data manipulation in Python. We can create a DataFrame from a list of simple tuples, and can even choose the specific elements of the tuples we want to use. Using pd.DataFrame() functionHere we will create a Pandas Dataframe using a list of tuples with the pd.DataFrame() function. Example 1: In this example, we will simply pass
2 min read
Python | Creating a Pandas dataframe column based on a given condition
While operating on data, there could be instances where we would like to add a column based on some condition. There does not exist any library function to achieve this task directly, so we are going to see how we can achieve this goal. In this article, we will see how to create a Pandas dataframe column based on a given condition in Python. Proble
8 min read
Creating a Pandas Series from Dictionary
A Pandas Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It has to be remembered that, unlike Python lists, a Series will always contain data of the same type. Let’s see how to create a Pandas Series from Python Dictionary. Pandas Series from Dictionary In
2 min read
Creating a Pandas Series from Lists
A Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It has to be remembered that unlike Python lists, a Series will always contain data of the same type. Let’s see how to create a Pandas Series from lists. Method #1 : Using Series() method without any argume
2 min read
Creating a Pandas Series
Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index. Labels need not be unique but must be a hashable type. The object supports both integer and label-based indexing and provides a host of methods for performing operations
4 min read
Creating Series from list, dictionary, and numpy array in Pandas
Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index. Pandas Series is nothing but a column in an excel sheet. In this article, we will see various ways of creating a series using different data types. Creating Series from l
2 min read
Creating A Time Series Plot With Seaborn And Pandas
In this article, we will learn how to create A Time Series Plot With Seaborn And Pandas. Let's discuss some concepts : Pandas is an open-source library that's built on top of NumPy library. It's a Python package that gives various data structures and operations for manipulating numerical data and statistics. It's mainly popular for importing and an
4 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
How to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()?
We might sometimes need a tidy/long-form of data for data analysis. So, in python's library Pandas there are a few ways to reshape a dataframe which is in wide form into a dataframe in long/tidy form. Here, we will discuss converting data from a wide form into a long-form using the pandas function stack(). stack() mainly stacks the specified index
4 min read
Replace values of a DataFrame with the value of another DataFrame in Pandas
In this article, we will learn how we can replace values of a DataFrame with the value of another DataFrame using pandas. It can be done using the DataFrame.replace() method. It is used to replace a regex, string, list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame method are get replaced with another value dynamically.
4 min read
Converting Pandas Dataframe To Dask Dataframe
In this article, we will delve into the process of converting a Pandas DataFrame to a Dask DataFrame in Python through several straightforward methods. This conversion is particularly crucial when dealing with large datasets, as Dask provides parallel and distributed computing capabilities, allowing for efficient handling of substantial data volume
3 min read
Pandas Dataframe rank() | Rank DataFrame Entries
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas DataFrame rank() method returns a rank of every respective entry (1 through n) along an axis of the DataFrame passed. The rank is retu
3 min read
Pandas DataFrame to_dict() Method | Convert DataFrame to Dictionary
Python is a great language for doing data analysis because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas .to_dict() method is used to convert a DataFrame into a dictionary of series or list-like data type depending on the orient parameter. Exam
3 min read
Pandas DataFrame assign() Method | Create new Columns in DataFrame
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. The Dataframe.assign() method assigns new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original one
4 min read
Python | Pandas DataFrame.fillna() to replace Null values in dataframe
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Just like the pandas dropna() method manages and rem
5 min read
Python | Pandas Dataframe/Series.head() method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas head() method is used to return top n (5 by default) rows of a data frame or series. Syntax: Dataframe.head(n=5) Parameters: n: i
2 min read
Python | Pandas Dataframe/Series.tail() method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas tail() method is used to return bottom n (5 by default) rows of a data frame or series. Syntax: Dataframe.tail(n=5) Parameters: n
2 min read
Python | Pandas Series/Dataframe.any()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas any() method is applicable both on Series and Dataframe. It checks whether any value in the caller object (Dataframe or series) i
3 min read