[英]how to write a function to find Zero value count and % of zero for all the columns and export into excel in python
I have one data set which has 780 columns and 87529 rows it contains lots of zero values.我有一个数据集,它有 780 列和 87529 行,它包含许多零值。 I am using the below code, but I am getting a 780*2 line as result, which is really difficult to read and understand,so i wanted to export this result into excel,can anyone help me to construct the code.
我正在使用下面的代码,但是我得到一个 780*2 的行,这真的很难阅读和理解,所以我想把这个结果导出到 excel,谁能帮我构建代码。
for column_name in df.columns:
column = df[column_name]
count = (column == 0).sum()
percent_zero = (column ==0 ).sum()/87529*100
print('Count of zeros in column ', column_name, ' is : ', count)
Try this one.试试这个。 (You have to use your own
df
) (您必须使用自己的
df
)
import pandas as pd
# Use your own dataframe.
df = pd.DataFrame([
{'col1': 0, 'col2': 0},
{'col1': 1, 'col2': 0},
{'col1': 1, 'col2': 1},
])
temp = 'Count of zeros in column "{col}" is : {n_zeros} (Percentage: {percent_zero:.1f}%)'
n_rows = len(df)
seeds = []
for col, ser in df.iteritems():
n_zeros = (ser == 0).sum()
percent_zero = n_zeros / n_rows * 100
print(temp.format(col=col, n_zeros=n_zeros, percent_zero=percent_zero))
seeds.append({'column_name': col, 'number_of_zero': n_zeros, 'percent_of_zero': percent_zero})
df_out = pd.DataFrame(seeds)
df_out.to_excel('out.xlsx', index=False)
If you got an error related to export, try this command:如果您遇到与导出相关的错误,请尝试以下命令:
pip install openpyxl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.