compution average in python: mean(),pivot_table()

来源:互联网 发布:ubuntu麒麟 下载 编辑:程序博客网 时间:2024/06/03 15:03
import pandas as pd


# This is the same value that we computed in the last screen, but it's much simpler.
# The ease of using the .mean() method is great, but it's important to understand how the underlying data looks.
correct_mean_age = titanic_survival["age"].mean()
correct_mean_fare=titanic_survival["fare"].mean()
print(correct_mean_age)

print(correct_mean_fare)


another way:

passenger_survival = titanic_survival.pivot_table(index="pclass", values="survived", aggfunc=np.mean)


# First class passengers had a much higher survival chance
print(passenger_survival)
passenger_age=titanic_survival.pivot_table(index="pclass",values="age",aggfunc=np.mean)
print(passenger_age)

0 0