Aka_Minimal
Aka_Minimal

Reputation: 35

Python 3.4, error when creating a DataFrame with panda

I'm attempting to create a DataFrame with the following:

from pandas import DataFrame, read_csv

import matplotlib.pyplot as plt
import pandas as pd
import sys

# The inital set of baby names and birth rates
names =['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]

#Now we wil zip them together
BabyDataSet = zip(names,births)
    ##we have to add the 'list' for version 3.x
print (list(BabyDataSet))

#create the DataFrame
df = DataFrame(BabyDataSet, columns = ['Names', 'Births'] )
print (df)

when I run the program I get the following error: 'data type can't be an iterator' I read the following, 'What does the "yield" keyword do in Python?', but I do not understand how that applies to what I'm doing. Any help and further understanding would be greatly appreciated.

Upvotes: 1

Views: 2405

Answers (2)

Roger Fan
Roger Fan

Reputation: 5045

You can also create the dataframe using an alternate syntax that avoids the zip/generator issue entirely.

df = DataFrame({'Names': names, 'Births': births})

Read the documentation on initializing dataframes. Pandas simply takes the dictionary, creates one column for each entry with the key as the name and the value as the value.

Dict can contain Series, arrays, constants, or list-like objects

Upvotes: 1

chrisb
chrisb

Reputation: 52276

In python 3, zip returns an iterator, not a list like it does in python 2. Just convert it to a list as you construct the DataFrame, like this.

df = DataFrame(list(BabyDataSet), columns = ['Names', 'Births'] )

Upvotes: 1

Related Questions