Creating a Pandas DataFrame from a Python Generator

Python

In this example we create a dataframe from a Python generator that takes numbers from 1 to 10 and also returns the square of those numbers.

 1|  import pandas
 2|  generator = ((i,i**2) for i in range(1,11))
 3|  df = pd.DataFrame(generator,columns=['number','number_squared'])
 4|  df.head(10)
Creating a Pandas DataFrame from a Python Generator
Did you find this snippet useful?

Sign up for free to to add this to your code library