pandas 面试题挑战十一

把DataFrame中'Min.Price','Max.Price'缺失的值用该列的均值填充

现有数据如下:

df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')

数据如下:

 

 把'Min.Price','Max.Price'缺失的值用该列的均值填充
解决方法如下:

df_out = df[[Min.Price',Max.Price']] = df[[']].apply(lambda x: x.fillna(x.mean()))
df_out

 

 

把DataFrame中'Min.Price','Max.Price'缺失的值用该列的均值, 中位数填充

现有数据如下:

df = pd.read_csv(')

数据如下:

 

 把'Min.Price','Max.Price'缺失的值用该列的均值,中位数填充
解决方法如下

d = {': np.nanmean,1)">': np.nanmedian}
df[[']].apply(lambda x,d : x.fillna(d[x.name](x)),args=(d,))
df

 

 

重点解读:
df[[']].apply(x.fillna(d[x.name]),args=(d,))appley中,可以通过args参数传递全局变量,本例中传递的是d,注意传递是元组,所以别忘了后面的“,”。

 

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...