Python Forum
Simple Series Keyword Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Series Keyword Error
#1
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
Reply
#2
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.
Reply
#3
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?)?
Reply
#4
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 *.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple numpy reshape error wih contour3D AdeIsHere 0 2,148 Sep-17-2019, 12:01 PM
Last Post: AdeIsHere
  Series object error message abhaydd 1 4,824 Aug-11-2019, 01:29 AM
Last Post: boring_accountant
  Newbie at using python and tensorflow getting error when running simple code FeatherineAu 0 3,962 Sep-28-2018, 02:09 PM
Last Post: FeatherineAu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020