Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'Series' is not defined

Tags:

python

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 "dataframe.py", 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!

like image 529
Aschharwood Avatar asked Jan 23 '16 18:01

Aschharwood


1 Answers

Alternatively, you may import the functions directly into your main namespace.

from pandas import Series, DataFrame

then you may skip prepending pd. to Series() and the DataFrame function calls every time you call them. In other words, you could run your original code exactly as you had it written.

like image 82
leerssej Avatar answered Sep 17 '22 19:09

leerssej