Iloc[] Grundlæggende
Du kan også få adgang til rækker i en DataFrame ved hjælp af deres indeks. Der er flere måder at gøre dette på:
.iloc- bruges til at få adgang til rækker via deres numeriske indeks, startende fra 0;.loc- bruges til at få adgang til rækker via deres strengetiket.
I dette kursus vil du udelukkende fokusere på at bruge attributten .iloc.
12345import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) print(countries)
DataFrame har følgende struktur:
Du kan se den første kolonne, som fungerer som rækkeindeks. Brug disse indekser til at tilgå specifikke rækker i DataFrame. Syntaksen for denne attribut er:
df.iloc[index]
Brug denne attribut til at tilgå den tredje og syvende række i DataFrame:
12345678import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) # Accessing to the third and seventh rows print(countries.iloc[2]) print(countries.iloc[6])
Efter at have kørt ovenstående kode, får du rækker, der svarer til de indekser, der er angivet i billedet nedenfor:
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain the difference between `.iloc` and `.loc` again?
How do I access multiple rows at once using `.iloc`?
What happens if I use an index that is out of range with `.iloc`?
Fantastisk!
Completion rate forbedret til 3.03
Iloc[] Grundlæggende
Stryg for at vise menuen
Du kan også få adgang til rækker i en DataFrame ved hjælp af deres indeks. Der er flere måder at gøre dette på:
.iloc- bruges til at få adgang til rækker via deres numeriske indeks, startende fra 0;.loc- bruges til at få adgang til rækker via deres strengetiket.
I dette kursus vil du udelukkende fokusere på at bruge attributten .iloc.
12345import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) print(countries)
DataFrame har følgende struktur:
Du kan se den første kolonne, som fungerer som rækkeindeks. Brug disse indekser til at tilgå specifikke rækker i DataFrame. Syntaksen for denne attribut er:
df.iloc[index]
Brug denne attribut til at tilgå den tredje og syvende række i DataFrame:
12345678import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) # Accessing to the third and seventh rows print(countries.iloc[2]) print(countries.iloc[6])
Efter at have kørt ovenstående kode, får du rækker, der svarer til de indekser, der er angivet i billedet nedenfor:
Tak for dine kommentarer!