Aschharwood Aschharwood - 1 year ago 312
Python Question

NameError: name 'Series' is not defined

I'm new to coding, currently trying the Udacity Data Science intro course. Trying to recreate an example in a lecture.

Here's the code:

import pandas as pd
import numpy as np

d = {
'name': Series(['Braund', 'Cummings', 'Heikkinen', 'Allen'], index=['a', 'b', 'c', 'd']),
'age': Series([22, 38, 26, 35], index=['a', 'b', 'c', 'd']),
'fare': Series([7.25, 71.83, 8.05], index=['a', 'b', 'd']),
'survived?': Series([False, True, True, False], index['a', 'b', 'c', 'd'])
df = DataFrame(d)
print df

Here's my error:

Traceback (most recent call last):
File "", line 4, in <module>
d = {'name': Series(['Braund', 'Cummings', 'Heikkinen', 'Allen'],
NameError: name 'Series' is not defined
Aschs-MacBook-Air:mystuff aschharwood$

I'm saving as a .py file and running in terminal.

Your help and guidance is much appreciated!

Answer Source

You've imported your module as namespace. Classes you are trying to use are not in your local namespace, but in the namespace of imported module.

Simply refer to a correct namespace - use pd.__WHAT_YOU_WANT__:

import pandas as pd

d = {'name': pd.Series(['Braund', 'Cummings', 'Heikkinen', 'Allen'],
index = ['a', 'b', 'c', 'd']), 'age': pd.Series([22, 38, 26, 35], 
index = ['a', 'b', 'c', 'd']), 'fare': pd.Series([7.25, 71.83, 8.05], index = ['a', 'b', 'd']),
'survived?': pd.Series([False, True, True, False], index['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

print df
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download