简体   繁体   English

如何编写 function 以查找所有列的零值计数和零百分比并导出到 python 中的 excel

[英]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.

相关问题 PySpark 写入 function 以计算给定列的非零值 - PySpark write a function to count non zero values of given columns Python - 使用 scipy 查找函数的零 - Python - find zero of a function with scipy 如何使用Python在Excel中删除所有零值 - How to delete all zero' values in excel using python Pandas:如何用该列的平均值替换列中的零值,对于所有具有零值的列 - Pandas: How to replace Zero values in a column with the mean of that column, For all columns with Zero Value 在列组中查找结果为非零值 - Find Result as Non Zero Value in Group of Columns 如何在python panda数据框中找到所有零单元格并替换它们? - How to find all the zero cells in a python panda dataframe and replace them? 返回零(计数)作为 Python 词典中的值 - return zero(count) as value in Python Dictionaries 计数为零时的熊猫分组以及如何在结果中包含零值 - pandas groupby when count is zero and how to include zero value in result 如何 output 计数来自 Spark dataframe 的两个二进制列的所有成对组合的计数,即使它是零计数? - How to output the count of all pairwise combination of two binary columns from a Spark dataframe even when it is zero count? 在字典中找到最小非零值(Python) - Find minimum non zero value in dictionary (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM