NameError: name 'Series' not defined

I am new to coding, am currently trying to implement the Udacity Data Science course. An attempt to recreate an example in a lecture.

Here is 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 is my mistake:

 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 save the .py file and run in the terminal.

Your help and guidance is greatly appreciated!

+5
source share
2 answers

You have imported your module into the namespace. The classes you are trying to use are not in your local namespace, but in the namespace of the imported module.

Just refer to the 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 
+9
source

In addition, you can import functions directly into the main namespace.

 from pandas import Series, DataFrame 

then you can skip adding pd. in Series() , and the DataFrame function calls every time you call them. In other words, you can run your original code exactly as you wrote it.

+2
source

Source: https://habr.com/ru/post/1241317/


All Articles