![]() |
Index Function not recognized in Python 3 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Index Function not recognized in Python 3 (/thread-39139.html) |
Index Function not recognized in Python 3 - Peter_B_23 - Jan-08-2023 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 RE: Index Function not recognized in Python 3 - deanhystad - Jan-08-2023 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']. |