Python Forum
Simple Series Keyword Error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Simple Series Keyword Error (/thread-1723.html)



Simple Series Keyword Error - Kris - Jan-22-2017

import pandas 
import numpy

#create python dictionary

d = { 'name' : Series ( ['Braund','Cummins','Heikkinen','Allen'], index ['1','2','3','4']),
      'age'  : Series ( ['22','38','26','35']                   , index ['1','2','3','4']),
      'fare' : Series ( ['7.25','71.83','8.05']                 , index ['1','2',    '4']),
 'Survived'  : Series ( ['F','T','T','F']                       , index ['1','2','3','4']),
    }
    
df = DataFrame(d)

i get an error , saying it doesnt recognize Series or Index keywords. I am using Spyder , Python 3.5. Can you help please


RE: Simple Series Keyword Error - micseydel - Jan-22-2017

When you import a module in Python, you have to either from module import thing or after import module refer to thing as module.thing. There's nowhere in your code that you're saying where Series, index, or Dataframe come from.

If you change your first import to
from pandas import Series
you'll get past your first problem.


RE: Simple Series Keyword Error - Kris - Jan-22-2017

Thank you so much , initially i tried with from pandas import * but that dint work too , shouldnt * take care of all Series ,index ,DataFrame.
Or should we specifiy each of the things(are these objects in the package pandas, the things?)?


RE: Simple Series Keyword Error - ichabod801 - Jan-22-2017

It's better to specify what you need, or to reference them from the module:

import pandas

x = pandas.Series(['F', 'T', 'T', 'F'])
If you do "from pandas import *" it will import everything. However, it may import things you have already defined, or are going to define, or are defined by other modules you did a * import from. For that reason, the more you import, the less you should import *.


RE: Simple Series Keyword Error - micseydel - Jan-22-2017

Python allows for star-imports, however in practice they're strongly discouraged. Imagine you star-import from pandas and numpy, it wouldn't be very clear in your code where Series, index, Dataframe, etc. come from. Worse yet, imagine both modules have something with the same name!

When trying to play with your code, it also seemed that index was not a class like Series and Dataframe, but rather was a sub-module. So I couldn't get your code to work, though I didn't try exhaustively. If you're still having trouble, please post the full stacktrace so we can get a good idea of what the current problem is.