提取a列b值下的c值
首先使用分组的思想,对a列b值下的c值进行整合观察。
pay = df1.groupby('*位')["c列变量名"].describe()
pay
这里想要补充的一点是其他的求分位数的方法。
其他求分位数的方法
n = np.percentile(z_df['天数'],(20,40), interpolation='midpoint')
print('天数分位数',a)
这句求解分位数的代码非常灵活,可以通过修改数值而达到自由求解不同分位数的方法。
第一列被作为了索引值无法引用变量怎么办?
得到描述性统计结果表如下所示,可以看到,结果的第一列被作为了索引值,导致在后续代码中想引用这个变量却无法引用。
解决办法:
复制索引列,新增一列变量:
cols = ['*位'] + list(df1.columns)
dangpay.index += 1
dangpay['*位'] = dangpay.index
取出来的值还是表格形式怎么办?
这个时候我想取【*位】为22的中位数值,得到的结果还是表格的形式,并不能赋值给某个变量。
解决办法:
因此需要对该语句加上后缀,只取其数值。只取值的情况,在变量搜索后加**.values**就好
res = dangpay[dangpay.*位==a][['50%']].values
或者
import numpy as np
new_res = res.astype(np.int)
取出的便只是数值啦!
填补空值的部分就很简单了。这里要注意的一点是,判断是否为空的语句形式:
if pd.isnull(df1.iloc[i,-1]) ==True
综合填补空值的部分为:
lis = []
for i in range (len(df1)):
if pd.isnull(df1.iloc[i,-1]) ==True:#倒数第一列变量
a = df1.iloc[i,5]#第五列变量
# 取出*位数为a的中位数值
zws = dangpay[dangpay.*位==a][['50%']].values
df1.iloc[i,-1] = int(zws)
lis.append(df1.iloc[i,-1])
print(df1.iloc[i,-1])
else:
lis.append(df1.iloc[i,-1])
print(df1.iloc[i,-1])
df1['aaa'] = lis