Untitled
unknown
plain_text
9 months ago
1.8 kB
19
Indexable
#Aufgabe 1a
import pandas as pd
countryData = {
"Wien": pd.Series(["Österreich", 414.6, 1805681],
index=["country", "area", "population"]),
"Berlin": pd.Series(["Deutschland", 891.85, 3562166],
index=["country", "area", "population"]),
"Zürich": pd.Series(["Schweiz", 87.88, 378884],
index=["country", "area", "population"])
}
df = pd.DataFrame(countryData)
ser_a = df.T.stack()
print(ser_a, "\n")
#Aufgabe 1b
ser_b = ser_a.sort_index()
print(ser_b, "\n")
#Aufgabe 1c
ser_c = ser_b.swaplevel(0, 1).sort_index()
print(ser_c)
#Aufgabe 2a
import pandas as pd
personData = {"Gewicht" : [65 ,58 ,58 ,45 ,43 ,99 ,68 ,60] ,
"Größe": [179, 165, 172 ,154 , 150 , 189, 176, 175]}
names= ["Henry" , "Sarah", "Elke", "Lulu", "Vera", "Toni","Maria", "Chris"]
personDF = pd.DataFrame(personData, index=names)
print(Gewicht_frame)
#Aufgabe 2b
import pandas as pd
bmi = personDF["Gewicht"] / ((personDF["Größe"] / 100) ** 2)
normal = personDF[(bmi >18.5) & (bmi < 25)]
print(normalbereich)
#Aufgabe 2c
filter = [n for n in personDF.index if "e" in n.lower()]
print(personDF.loc[filter])
#Aufgabe 2d
personDF["BMI"] = round(
personDF["Gewicht"] / ( (personDF["Größe"] / 100) ** 2 ),
6
)
print(personDF)
#Aufgabe 2e
personDF["BMI"] = round(
personDF["Gewicht"] / ( (personDF["Größe"] / 100) ** 2 ),
6
)
print(personDF.sort_values(by="BMI", ascending=False))
#Aufgabe 2f
personDF["BMI"] = round(
personDF["Gewicht"] / ( (personDF["Größe"] / 100) ** 2 ),
6
)
filter = [n for n in personDF.index if "e" in n]
print(personDF.loc[filter].query("BMI<20"))Editor is loading...
Leave a Comment