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 :]) |
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 ]) |
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 ]]) |
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’ , ’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
$FREE
Best Python online courses for 2022
$FREE
Best laptop for Fortnite
$399+
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 answers
Wiki
Python | How to copy data from one Excel sheet to another
Common xlabel/ylabel for matplotlib subplots
Check if one list is a subset of another in Python
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
Python add suffix / add prefix to strings in a list
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
Python - Move item to the end of the list
Python - Print list vertically