Sha*_*ata 8 python numpy python-3.x pandas
import pandas as pd
df = pd.read_csv('https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0')
percent= 100*(len(df.loc[:,df.isnull().sum(axis=0)>=1 ].index) / len(df.index))
print(round(percent,2))
Run Code Online (Sandbox Code Playgroud)
输入是https://query.data.world/s/Hfu_PsEuD1Z_yJHmGaxWTxvkz7W_b0
输出应该是
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 1.30
dtype: float64
Run Code Online (Sandbox Code Playgroud)
Eng*_*ero 21
这个怎么样?我想我之前在这里曾经发现了类似的东西,但我现在还没有看到它......
percent_missing = df.isnull().sum() * 100 / len(df)
missing_value_df = pd.DataFrame({'column_name': df.columns,
'percent_missing': percent_missing})
Run Code Online (Sandbox Code Playgroud)
如果您希望将缺失的百分比排序,请按照上述步骤操作:
missing_value_df.sort_values('percent_missing', inplace=True)
Run Code Online (Sandbox Code Playgroud)
正如评论中所提到的,你也可以在我上面的第一个代码中仅使用第二行,即:
percent_missing = df.isnull().sum() * 100 / len(df)
Run Code Online (Sandbox Code Playgroud)
bit*_*ang 13
单线解决方案
df.isnull().mean().round(4).mul(100).sort_values(ascending=False)
Run Code Online (Sandbox Code Playgroud)
Sco*_*ton 11
更新让我们使用mean同isnull:
df.isnull().mean() * 100
Run Code Online (Sandbox Code Playgroud)
输出:
Ord_id 0.000000
Prod_id 0.000000
Ship_id 0.000000
Cust_id 0.000000
Sales 0.238124
Discount 0.654840
Order_Quantity 0.654840
Profit 0.654840
Shipping_Cost 0.654840
Product_Base_Margin 1.297774
dtype: float64
Run Code Online (Sandbox Code Playgroud)
IIUC:
df.isnull().sum() / df.shape[0] * 100.00
Run Code Online (Sandbox Code Playgroud)
输出:
Ord_id 0.000000
Prod_id 0.000000
Ship_id 0.000000
Cust_id 0.000000
Sales 0.238124
Discount 0.654840
Order_Quantity 0.654840
Profit 0.654840
Shipping_Cost 0.654840
Product_Base_Margin 1.297774
dtype: float64
Run Code Online (Sandbox Code Playgroud)
要覆盖所有缺失的值并舍入结果:
((df.isnull() | df.isna()).sum() * 100 / df.index.size).round(2)
Run Code Online (Sandbox Code Playgroud)
输出:
Out[556]:
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 1.30
dtype: float64
Run Code Online (Sandbox Code Playgroud)