![]() |
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 PythonA generator function in Python is defined like a regular function, using the Syntax:
Here, F Example:In this example, we create a simple Python generator that yields 3 alphabets. Then we print these alphabets using Python for loop. Python3
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 DataFrameWe 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
Output: Numbers
0 0
1 1
2 2
3 3
4 4
Create a List from Generator expression and Making DataFrameIn this example, the generator expression Then, we use the We created a dictionary and then converted to list using list() function. Python3
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. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |