Python Forum
Index Function not recognized in Python 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index Function not recognized in Python 3
#1
Hi Everyone,
I'm learning Python from an Online course and all was going well until I got to the subject of 'Series'.
There are some exercises where Series is Used with Index & it throws an error as described below.
The course uses Python 2 and I've tried to find the equivalent of Index from Python 2 to Python 3 with no luck.
Here's my script Using Jupyter Notebooks from Anaconda and Windows 10 o/s.
Any suggestions appreciated.

import numpy as np
from pandas import Series, DataFrame
import pandas as pd

my_ser = Series([1,2,3,4],index['A','B','C','D'])

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21480/685396208.py in <module>
----> 1 my_ser = Series([1,2,3,4],index['A','B','C','D'])

NameError: name 'index' is not defined
Reply
#2
There error is correct. There is no variable or function named index, nor is index['A', 'B', 'C', 'D"] valid for any kind of python I know about.
[python]import pandas as pd

my_ser = pd.Series([1, 2, 3, 4], index=['A', 'B', 'C', 'D'])
index is an optional argument when you create a Series. index=['A', 'B', 'C', 'D'] is treating index as a named argument and passing the value ['A', 'B', 'C', 'D'].
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 672 Jan-20-2024, 09:20 PM
Last Post: Learner1
  is import cointegration_analysis a recognized module mitcht33 1 438 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  The term 'pip' is not recognized as the name of a cmdlet, function michaelnicol 1 637 Jul-16-2023, 11:12 PM
Last Post: deanhystad
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 1,978 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  min() function in iterable index OokaydO 4 2,983 Apr-23-2020, 09:21 AM
Last Post: OokaydO
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 2,541 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  matplotlib isn't recognized after installation Pavel_47 5 2,852 Sep-18-2019, 07:01 PM
Last Post: snippsat
  how do i get y to be recognized in this comprehension? Skaperen 5 3,142 Aug-26-2019, 07:43 PM
Last Post: Skaperen
  why is my dictionary not recognized as such zatlas1 5 3,791 Jan-14-2019, 01:15 AM
Last Post: woooee
  pyserial-master installed bbut not recognized elwolv1 0 1,992 Jan-04-2019, 08:37 PM
Last Post: elwolv1

Forum Jump:

User Panel Messages

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