pandas 3

pandas 3

Making Pivot Tables

Pivot tables provide an easy way to subset by one column and then apply a calculation like a sum or a mean.

Pivot tables first group and then apply a calculation. In the previous screen, we actually made a pivot table manually by grouping by the column "pclass" and then calculating the mean of the "fare" column for each class.

Luckily, we can use the Dataframe.pivot_table() method instead, which simplifies the kind of work we did on the last screen. To produce the same data, we could use one line.

passenger_class_fares =titanic_survival.pivot_table(index="pclass", values="fare", aggfunc=np.mean)

The first parameter of the method, index tells the method which column to group by.

The second parameter values is the column that we want to apply the calculation to, and aggfunc specifies the calculation we want to perform.

The default for the aggfunc parameter is actually the mean, so if we're calculating this we can omit this parameter.

Instructions

  • Use the DataFrame.pivot_table() method to calculate the mean age for each passenger class ("pclass").
  • Assign the result to passenger_age.
  • Display the passenger_age pivot table using the print() function.
import numpy as np

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

passenger_age =titanic_survival.pivot_table(index="pclass", values="age")

print(passenger_age)

If we pass a list of column names to the values parameter instead of a single value, we can perform calculations on multiple columns at once.

We can also specify a custom calculation to be made. For instance, if we pass np.sum to the aggfunc parameter it will total the values in each column.

Instructions

  • Make a pivot table that calculates the total fares collected ("fare") and total number of survivors ("survived") for each embarkation port ("embarked").
  • Assign the result to port_stats.
  • Display port_stats using the print() function.
import numpy as np

port_stats =titanic_survival.pivot_table(index = 'embarked',values = ['fare',"survived"],aggfunc= numpy.sum)
  
print(port_stats)



Drop Missing Values

We can use the DataFrame.dropna() method on pandas DataFrames to do this. The method will drop any rows that contain missing values.

The dropna() method takes an axis parameter, which indicates whether you would like to drop rows or columns.

Specifying axis=0 or axis='index' will drop any rows that have null values, while specifying axis=1 or axis='columns' will drop any columns that have null values.

Instructions

Drop all columns in titanic_survival that have missing values and assign the result to drop_na_columns.
Drop all rows in titanic_survival where the columns "age" or "sex" have missing values and assign the result to new_titanic_survival.

drop_na_columns =titanic_survival.dropna(axis = 1)

new_titanic_survival = titanic_survival.dropna(axis =0,subset=['sex','age'])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 从几何时,讨厌一个人外出;一些琐事,不习惯一个人去做,可以为了不想一个人出门,而去约各种闺蜜,独自一个人真...
    小洛米阅读 200评论 0 1
  • 本周新片丨影指点点 ▼ 《速8》一来,影市如期“井喷”! 本周2017年第16周(2017年4月17日~2017年...
    影指点点阅读 371评论 0 1
  • #幸福是需要修出来的~每天进步1%~幸福实修13班~08李玉珍# 20171129(2/60) 【幸福三朵玫瑰】 ...
    stx2010阅读 215评论 2 3
  • 師走 晚冬 春待月 樹梢已空枝 落葉化成雪 花苞雪中正待時
    FukuzawaJoel阅读 291评论 1 0
  • 前几天看到有人朋友圈发状态,大概意思是周围的闺蜜,朋友,同学都结婚生娃了,想找个出去玩的人都难。然后我蓦然发...
    卜哒灵阅读 2,781评论 5 17

友情链接更多精彩内容