Series
Importing Pandas
Before we can start working with Pandas, we need to import it:
import pandas as pd
Defining a Series
We can define a Series in Pandas using the following syntax:
pd.Series(elements_list, index_list)
The following code defines a new Series called populations
representing the populations of various cities (in milions):
12345678import pandas as pd populations = pd.Series( [8.9, 3.6, 2.7, 1.6], ["London", "Berlin", "Rome", "Budapest"] ) print(populations)
In some cases you might see the following syntax of creating a Series, which has the same effect, except in this case we use a named parameter to specify the index_list
for the sake of readibility:
populations = pd.Series(
[8.9, 3.6, 2.7, 1.6],
index=["London", "Berlin", "Rome", "Budapest"]
)
The index
Property
We can access the list of indices of a Series using the index
property:
12345678import pandas as pd populations = pd.Series( [8.9, 3.6, 2.7, 1.6], ["London", "Berlin", "Rome", "Budapest"] ) print(populations.index)
1. What does the following code create?
2. What will be the output of the following code?
3. You must always specify the index parameter when creating a Series.
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Kysy minulta kysymyksiä tästä aiheesta
Tiivistä tämä luku
Näytä käytännön esimerkkejä
Awesome!
Completion rate improved to 2.7
Series
Pyyhkäise näyttääksesi valikon
Importing Pandas
Before we can start working with Pandas, we need to import it:
import pandas as pd
Defining a Series
We can define a Series in Pandas using the following syntax:
pd.Series(elements_list, index_list)
The following code defines a new Series called populations
representing the populations of various cities (in milions):
12345678import pandas as pd populations = pd.Series( [8.9, 3.6, 2.7, 1.6], ["London", "Berlin", "Rome", "Budapest"] ) print(populations)
In some cases you might see the following syntax of creating a Series, which has the same effect, except in this case we use a named parameter to specify the index_list
for the sake of readibility:
populations = pd.Series(
[8.9, 3.6, 2.7, 1.6],
index=["London", "Berlin", "Rome", "Budapest"]
)
The index
Property
We can access the list of indices of a Series using the index
property:
12345678import pandas as pd populations = pd.Series( [8.9, 3.6, 2.7, 1.6], ["London", "Berlin", "Rome", "Budapest"] ) print(populations.index)
1. What does the following code create?
2. What will be the output of the following code?
3. You must always specify the index parameter when creating a Series.
Kiitos palautteestasi!