joyful pandas task7-缺失数据

1.缺失数据可以使用isna或isnull
1.同时对几个列,检索出全部为缺失或者至少有一个缺失或者没有缺失的行
sub_set = df[[‘Height’, ‘Weight’, ‘Transfer’]]
df[sub_set.isna().all(1)] # 全部缺失
2.df[sub_set.isna().any(1)].head() # 至少有一个缺失
3.df[sub_set.notna().all(1)].head() # 没有缺失

2.ropna的主要参数为轴方向axis(默认为0,即删除行)、删除方式how、阈值thresh、备选的删除子集subset。
1.删除身高体重至少有一个缺失的行:
res = df.dropna(how = ‘any’, subset = [‘Height’, ‘Weight’])
2.删除超过15个缺失值的列:
res = df.dropna(1, thresh=df.shape[0]-15) # 身高被删除

3.利用fillna进行填充
1.有时为了更加合理地填充,需要先进行分组后再操作。例如,根据年级进行身高的均值填充:
df.groupby(‘Grade’)[‘Height’].transform(lambda x: x.fillna(x.mean())).head()

4.插值函数
1.最近邻插补,即缺失值的元素和离它最近的非缺失值元素一样:
s.interpolate(‘nearest’).values

5.缺失数据的计算和分组
1.当调用函数sum, prob使用加法和乘法的时候,缺失数据等价于被分别视作0和1,即不改变原来的计算结果:
2.当使用累计函数时,会自动跳过缺失值所处的位置:
3.当调用函数sum, prob使用加法和乘法的时候,缺失数据等价于被分别视作0和1,即不改变原来的计算结果:

转:joyful pandas

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值