Bottom Page

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
Quote
#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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#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?)?
Quote
#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

Quote
#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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote

Top Page

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

Forum Jump:


Users browsing this thread: 1 Guest(s)