Reputation: 391
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!
Upvotes: 11
Views: 31514
Reputation: 14988
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.
Upvotes: 7
Reputation: 23233
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
Upvotes: 17