In this article we use the file β nba.csv
"to download CSV, click here .
Access to an element from a series with a position
An index number is used to access an element of a series. Use the index operator [] to access an item in a series. The index must be an integer.
To access several elements from the series, we use the Slice operation. The Slice operation is performed on a series using the colon (:). To print items from start to range, use [:Index ]
to print items from end use [:-Index ]
, to print items from a specific index to the end, use [Index:]
to print items within a range use [Start Index: End Index] and to print an entire series using a slicing operation use [:>
. Next, to print the entire series in reverse order, use [::-1 ]
.
Code # 1: Accessing the first element of the series
# pandas and nudies import import pandas as pd import numpy as np Β # create a simple array data = np.array ([ βgβ , β eβ , βeβ , β kβ , βsβ , β fβ , βoβ , βrβ , β gβ , βeβ , βeβ , β kβ , βsβ ]) ser = pd.Series (data) Β Β # get first element print (ser [ 0 ] ) |
Output:
g
Code no. 2: Access the first 5 elements of the series
# import of pandas and nudies import pandas as pd import numpy as np Β Β # create a simple array data = np.array ([ βgβ , β eβ , βeβ , β kβ , βsβ , β fβ , βoβ , βrβ , β gβ , βeβ , β eβ , βkβ , β sβ ]) ser = pd.Series (data) Β Β # get first element print (ser [: 5 ]) |
Output:
Code # 3: Access to the last 10 elements of the series
# pandas and nudies import import pandas as pd import numpy as np Β # create a simple array data = np.array ([ βgβ , β eβ , βeβ , βkβ , βsβ , β fβ , βoβ , β rβ , β gβ , βeβ , βeβ , βkβ , β sβ ]) ser = pd.Series (data) Β Β # get the first element print (ser [ - 10 :]) |
tbody>
Output:
Code # 4: Accessing the first 5 Series elements in nba.csv
# pandas module import import pandas as pdΒ Β # create data frame df = pd.read_csv ( "nba.csv" )Β Β ser = pd.Series (df [ βNameβ ])Β ser.head ( 10 )Β |
We now access the first 5 e series elements
# get the first five names ser [: 5 ]Β |
Output:
Accessing an element by label (index)
To access an element from a series, we have to set values ββby the index label. A series is like a fixed-size dictionary where you can get and set values ββby an index mark.
Code # 1: Accessing a single item using an index mark
# pandas and nudies import import pandas as pd import numpy as np Β # create a simple array data = np.array ([ βgβ , βeβ , β eβ , βkβ , β sβ , βfβ , βoβ , β rβ , β gβ , βeβ , βeβ , β kβ , βsβ ]) ser = pd.Series (data, index = [ 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 ]) Β Β # access the element using the index element print ( ser [ 16 ]) |
table>
Output:
o
Code # 2: Accessing multiple elements with an index mark
# pandas and nudies import import pandas as pd import numpy as np Β # create a simple array data = np.array ([ βgβ , β eβ , β eβ , βkβ , βsβ , βfβ , β oβ , βrβ , β gβ , βeβ , β eβ , βkβ , β sβ ]) ser = pd.Series (data, index = [ 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 ]) Β Β # access multiple items with # index element print (ser [[ 10 , 11 , 12 , 13 , 14 ]]) | tr>
Output:
Code # 3: Access to multiple items by providing an index label
# pandas and numpy imports import pandas as pdΒ import numpy as npΒ Β ser = pd.Series (np.arange ( 3 , 9 ), index = [ βaβ , β bβ , βcβ , β dβ , βeβ , cod e> βfβ ])Β Β Β print (ser [[ βaβ , βdβ , βgβ , β lβ ]]) |
Output:
Code # 4: Multiple access elements with an index mark in nba.csv
# pandas module import import pandas as pdΒ Β # create data frame df = pd.read_csv ( "nba.csv" )Β Β ser = pd.Series (df [ βNameβ ])Β ser.head ( 10 )Β |
We now access multiple elements using the index mark
Output:
Shop
Learn programming in R: courses
$
Best Python online courses for 2022
$
Best laptop for Fortnite
$
Best laptop for Excel
$
Best laptop for Solidworks
$
Best laptop for Roblox
$
Best computer for crypto mining
$
Best laptop for Sims 4
$
Latest questions
NUMPYNUMPY
psycopg2: insert multiple rows with one query
12 answers
NUMPYNUMPY
How to convert Nonetype to int or string?
12 answers
NUMPYNUMPY
How to specify multiple return types using type-hints
12 answers
NUMPYNUMPY
Javascript Error: IPython is not defined in JupyterLab
12 answers
Wiki
Python OpenCV |Β cv2.putText () method
numpy.arctan2 () in Python
Python |Β os.path.realpath () method
Python OpenCV |Β cv2.circle () method
Python OpenCVΒ cv2.cvtColor () method
Python -Β Move item to the end of the list
time.perf_counter () function in Python
Check if one list is a subset of another in Python
Python os.path.join () method