Create A Pandas Dataframe From Generator
Last Updated :
27 Jan, 2024
A Pandas DataFrame is a 2D data structure like a table with rows and columns. The values in the data frame are mutable and can be modified. Data frames are mostly used in data analysis and data manipulation. DataFrames store data in table form like databases and Excel sheets. We can easily perform arithmetic operations on them.There are many ways to create data frames. In this article, we will look into creating a pandas data frame from a generator function data frame.
Generator Function in Python
A generator function in Python is defined like a regular function, using the def
keyword. However, instead of using return
to send a value back to the caller, it employs the yield
keyword. The presence of the yield
statement within a function is what transforms it into a generator function.
Syntax:
def Func_Name():
yield statement
Here, Func_name
is the name of the generator function, and the yield
statement is where the function temporarily pauses, saving its state and allowing the generator to produce a value
Example:
In this example, we create a simple Python generator that yields 3 alphabets. Then we print these alphabets using Python for loop.
Python3
# A generator function that yields A for first time,
# B second time and C third time
def simple_generator():
yield 'A'
yield 'B'
yield 'C'
# Driver code to check above generator function
for value in simple_generator():
print(value)
Output:
A
B
C
I think you might understand how a generator function works. Now, let's learn how we can create Pandas Dataframes from the Generator function.
Creating a List from Generator() function and Making DataFrame
We are creating a DataFrame by using dataframe() function from the pandas library and passing a list that was created by a generator function.
Here, we are generating a list using the generator() function through a for loop, which creates a sequence of 5 numbers and then we are creating a DataFrame with the list using the pandas DataFrame() function.
Python3
# Generator function to create a sequence of numbers
def number_generator(n):
for i in range(n):
yield i
# Creating a list from the generator function
number_list = list(number_generator(5))
# Creating a DataFrame from the list
df = pd.DataFrame({'Numbers': number_list})
# Display the DataFrame
print(df)
Output:
Numbers
0 0
1 1
2 2
3 3
4 4
Create a List from Generator expression and Making DataFrame
In this example, the generator expression [chr(ord('A') + i) for i in range(5)]
is used within a for loop to create a list (alphabet_list
) containing alphabets from 'A' to 'E'.
Then, we use the pd.DataFrame()
function to create a DataFrame (df
) from the list, and the resulting DataFrame has one column named 'Alphabets' with values 'A' to 'E'.
We created a dictionary and then converted to list using list() function.
Python3
# Generator expression within a for loop to create a list of alphabets
alphabet_list = [chr(ord('A') + i) for i in range(5)]
# Creating a DataFrame from the list
df = pd.DataFrame({'Alphabets': alphabet_list})
# Display the DataFrame
print(df)
Output:
Alphabets
0 A
1 B
2 C
3 D
4 E
Conclusion:
The generator() function in Python is highly useful for generating values. In this article, we discussed creating a Pandas DataFrame using the generator() function and generator expression. With this tutorial, you will be to create DataFrames using both the generator function and generator expression.
Similar Reads
Create a list from rows in Pandas dataframe Python lists are one of the most versatile data structures, offering a range of built-in functions for efficient data manipulation. When working with Pandas, we often need to extract entire rows from a DataFrame and store them in a list for further processing. Unlike columns, which are easily access
4 min read
Create empty dataframe in Pandas The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use.Empty DataFrame could be created with the help
1 min read
Creating a Pandas DataFrame Pandas DataFrame comes is a powerful tool that allows us to store and manipulate data in a structured way, similar to an Excel spreadsheet or a SQL table. A DataFrame is similar to a table with rows and columns. It helps in handling large amounts of data, performing calculations, filtering informati
2 min read
Different ways to create Pandas Dataframe It is the most commonly used Pandas object. The pd.DataFrame() function is used to create a DataFrame in Pandas. There are several ways to create a Pandas Dataframe in Python.Example: Creating a DataFrame from a DictionaryPythonimport pandas as pd # initialize data of lists. data = {'Name': ['Tom',
7 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 Datafr
2 min read
Pandas DataFrame assign() Method - Create new Columns in DataFrame The assign() method in Pandas is used to create or modify columns in a DataFrame while preserving the original DataFrame. It returns a new DataFrame with the specified modifications. This method allows adding single or multiple columns, performing calculations using existing columns and applying fun
4 min read