Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用列中的平均值输入NAN,并找出缺失值的百分比_Python_Pandas - Fatal编程技术网

Python 用列中的平均值输入NAN,并找出缺失值的百分比

Python 用列中的平均值输入NAN,并找出缺失值的百分比,python,pandas,Python,Pandas,我想计算列Product\u Base\u Margin中所有缺失值的平均值,然后打印每列缺失值的百分比 我当前的代码: import numpy as np import pandas as pd df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0') df = df[~np.isnan(df['Product_Base_Margin'])] print(round(100*(df.isnu

我想计算列
Product\u Base\u Margin
中所有缺失值的平均值,然后打印每列缺失值的百分比

我当前的代码:

import numpy as np
import pandas as pd
df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0')


df = df[~np.isnan(df['Product_Base_Margin'])]
print(round(100*(df.isnull().sum()/len(df.index)), 2))
预期产出:

Ord_id                 0.00
Prod_id                0.00
Ship_id                0.00
Cust_id                0.00
Sales                  0.24
Discount               0.65
Order_Quantity         0.65
Profit                 0.65
Shipping_Cost          0.65
Product_Base_Margin    0.00
dtype: float64

我做错了什么?

我使用了
matplotlib
只是为了更好地显示结果。。。通过使用
isnull()
/
isna()

插入的意思是它的位置<代码>fillna()


当前代码的结果是什么?您想要的结果是什么?上述问题的预期输出是订单id 0.00产品id 0.00发货id 0.00客户id 0.00销售0.24折扣0.65订单数量0.65利润0.65运输成本0.65产品基本利润0.00数据类型:float64,因此只需计算/打印它。如果你知道答案,不确定你的问题是什么!
import numpy as np
import pandas as pd
df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0')
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,2, figsize=[10,5],
                      sharey=True, sharex=False)


df.loc[:,"Product_Base_Margin"].isnull().to_frame().value_counts().plot(ax=ax[0], kind="bar")
df.loc[:,"Product_Base_Margin"].fillna(df.loc[:,"Product_Base_Margin"].mean(), inplace=True)
df.loc[:,"Product_Base_Margin"].isnull().to_frame().value_counts().plot(ax=ax[1], kind="bar")