Accessing Elements from a Series
Default Indexing
We can access elements via their numerical (default) indices using the usual syntax series_name[index]
:
12345678import pandas as pd temperatures = pd.Series( [22, 18, 25, 19] ) # Access the 3rd element print(temperatures[2])
If the Series has Custom Indices, then it's recommended to use the iloc
property for indexing:
123456789import pandas as pd temperatures = pd.Series( [22, 18, 25, 19], index=["Monday", "Tuesday", "Wednesday", "Thursday"] ) # Accessing the 3rd element print(temperatures.iloc[2])
Custom Indexing
The syntax of Custom Indexing is very similar to accessing elements of a dictionary:
123456789import pandas as pd temperatures = pd.Series( [22, 18, 25, 19], index=["Monday", "Tuesday", "Wednesday", "Thursday"] ) # Accessing the temperature on "Wednesday" print(temperatures["Wednesday"])
Slicing with Default Indices
We can slice a Series using the syntax:
series_name[start:end]
It's important to note that the in case of default indices, the end
is exclusive, which can be better understood with the following example:
1234567891011import pandas as pd temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # The index "6" is excluded weekdays = temperatures.iloc[0:6] print(weekdays)
Slicing with Custom Indices
We can slice a series using it's custom indices too. In this case the syntax is the same as before, except the end
element is also included in the slice:
1234567891011import pandas as pd temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # "Friday" is included in the slice weekdays = temperatures['Monday':'Friday'] print(weekdays)
1. What will be the output of the following code?
2. Which method is recommended to access elements by position when using custom indices?
3. What is the output of this code?
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 2.7
Accessing Elements from a Series
Pyyhkäise näyttääksesi valikon
Default Indexing
We can access elements via their numerical (default) indices using the usual syntax series_name[index]
:
12345678import pandas as pd temperatures = pd.Series( [22, 18, 25, 19] ) # Access the 3rd element print(temperatures[2])
If the Series has Custom Indices, then it's recommended to use the iloc
property for indexing:
123456789import pandas as pd temperatures = pd.Series( [22, 18, 25, 19], index=["Monday", "Tuesday", "Wednesday", "Thursday"] ) # Accessing the 3rd element print(temperatures.iloc[2])
Custom Indexing
The syntax of Custom Indexing is very similar to accessing elements of a dictionary:
123456789import pandas as pd temperatures = pd.Series( [22, 18, 25, 19], index=["Monday", "Tuesday", "Wednesday", "Thursday"] ) # Accessing the temperature on "Wednesday" print(temperatures["Wednesday"])
Slicing with Default Indices
We can slice a Series using the syntax:
series_name[start:end]
It's important to note that the in case of default indices, the end
is exclusive, which can be better understood with the following example:
1234567891011import pandas as pd temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # The index "6" is excluded weekdays = temperatures.iloc[0:6] print(weekdays)
Slicing with Custom Indices
We can slice a series using it's custom indices too. In this case the syntax is the same as before, except the end
element is also included in the slice:
1234567891011import pandas as pd temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # "Friday" is included in the slice weekdays = temperatures['Monday':'Friday'] print(weekdays)
1. What will be the output of the following code?
2. Which method is recommended to access elements by position when using custom indices?
3. What is the output of this code?
Kiitos palautteestasi!