2 Upvotes

Creating a Pandas DataFrame from a Python Generator

Python
Import and Export

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.

import pandas
generator = ((i,i**2) for i in range(1,11))
df = pd.DataFrame(generator,columns=['number','number_squared'])
df.head(10)
{{ snippet.title}

By analyseup - Last Updated Sept. 22, 2021, 4:21 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Top Contributors
103
100