Python Forum

Full Version: Index Function not recognized in Python 3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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'].