Python Pandas:打印缺少值的列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37366717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:18:02  来源:igfitidea点击:

Pandas: print column name with missing values

pythonpandas

提问by LinearLeopard

I am trying to print or to get list of columns name with missing values. E.g.

我正在尝试打印或获取缺少值的列名称列表。例如

data1 data2 data3  
1     3     3  
2     NaN   5  
3     4     NaN  

I want to get ['data2', 'data3']. I wrote following code:

我想得到 ['data2', 'data3']。我写了以下代码:

print('\n'.join(map(
    lambda x : str(x[1])
    ,(filter(lambda z: z[0] != False, zip(train.isnull().any(axis=0), train.columns.values)))
)))

It works well, but I think should be simpler way.

它运作良好,但我认为应该是更简单的方法。

回答by ayhan

df.isnull().any()generates a boolean array (True if the column has a missing value, False otherwise). You can use it to index into df.columns:

df.isnull().any()生成一个布尔数组(如果列有缺失值则为真,否则为假)。您可以使用它来索引df.columns

df.columns[df.isnull().any()]

will return a list of the columns which have missing values.

将返回具有缺失值的列的列表。



df = pd.DataFrame({'A': [1, 2, 3], 
                   'B': [1, 2, np.nan], 
                   'C': [4, 5, 6], 
                   'D': [np.nan, np.nan, np.nan]})

df
Out: 
   A    B  C   D
0  1  1.0  4 NaN
1  2  2.0  5 NaN
2  3  NaN  6 NaN

df.columns[df.isnull().any()]
Out: Index(['B', 'D'], dtype='object')

df.columns[df.isnull().any()].tolist()  # to get a list instead of an Index object
Out: ['B', 'D']

回答by Vedang Mehta

Oneliner -

单线 -

[col for col in df.columns if df[col].isnull().any()]

回答by piRSquared

Another alternative:

另一种选择:

df.loc[:, df.isnull().any()]

回答by Suhas_Pote

import numpy as np
import pandas as pd

raw_data = {'first_name': ['Jason', np.nan, 'Tina', 'Jake', 'Amy'], 
        'last_name': ['Miller', np.nan, np.nan, 'Milner', 'Cooze'], 
        'age': [22, np.nan, 23, 24, 25], 
        'sex': ['m', np.nan, 'f', 'm', 'f'], 
        'Test1_Score': [4, np.nan, 0, 0, 0],
        'Test2_Score': [25, np.nan, np.nan, 0, 0]}
results = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'sex', 'Test1_Score', 'Test2_Score'])


results 
'''
  first_name last_name   age  sex  Test1_Score  Test2_Score
0      Jason    Miller  22.0    m          4.0         25.0
1        NaN       NaN   NaN  NaN          NaN          NaN
2       Tina       NaN  23.0    f          0.0          NaN
3       Jake    Milner  24.0    m          0.0          0.0
4        Amy     Cooze  25.0    f          0.0          0.0
'''

You can use following function, which will give you output in Dataframe

您可以使用以下功能,它将在数据框中为您提供输出

  • Zero Values
  • Missing Values
  • % of Total Values
  • Total Zero Missing Values
  • % Total Zero Missing Values
  • Data Type
  • 零值
  • 缺失值
  • 占总价值的百分比
  • 总零缺失值
  • % 总零缺失值
  • 数据类型

Just copy and paste following function and call it by passing your pandas Dataframe

只需复制并粘贴以下函数并通过传递您的熊猫数据框来调用它

def missing_zero_values_table(df):
        zero_val = (df == 0.00).astype(int).sum(axis=0)
        mis_val = df.isnull().sum()
        mis_val_percent = 100 * df.isnull().sum() / len(df)
        mz_table = pd.concat([zero_val, mis_val, mis_val_percent], axis=1)
        mz_table = mz_table.rename(
        columns = {0 : 'Zero Values', 1 : 'Missing Values', 2 : '% of Total Values'})
        mz_table['Total Zero Missing Values'] = mz_table['Zero Values'] + mz_table['Missing Values']
        mz_table['% Total Zero Missing Values'] = 100 * mz_table['Total Zero Missing Values'] / len(df)
        mz_table['Data Type'] = df.dtypes
        mz_table = mz_table[
            mz_table.iloc[:,1] != 0].sort_values(
        '% of Total Values', ascending=False).round(1)
        print ("Your selected dataframe has " + str(df.shape[1]) + " columns and " + str(df.shape[0]) + " Rows.\n"      
            "There are " + str(mz_table.shape[0]) +
              " columns that have missing values.")
#         mz_table.to_excel('D:/sampledata/missing_and_zero_values.xlsx', freeze_panes=(1,0), index = False)
        return mz_table

missing_zero_values_table(results)

Output

输出

Your selected dataframe has 6 columns and 5 Rows.
There are 6 columns that have missing values.

             Zero Values  Missing Values  % of Total Values  Total Zero Missing Values  % Total Zero Missing Values Data Type
last_name              0               2               40.0                          2                         40.0    object
Test2_Score            2               2               40.0                          4                         80.0   float64
first_name             0               1               20.0                          1                         20.0    object
age                    0               1               20.0                          1                         20.0   float64
sex                    0               1               20.0                          1                         20.0    object
Test1_Score            3               1               20.0                          4                         80.0   float64

If you want to keep it simple then you can use following function to get missing values in %

如果你想保持简单,那么你可以使用以下函数来获取 % 中的缺失值

def missing(dff):
    print (round((dff.isnull().sum() * 100/ len(dff)),2).sort_values(ascending=False))


missing(results)
'''
Test2_Score    40.0
last_name      40.0
Test1_Score    20.0
sex            20.0
age            20.0
first_name     20.0
dtype: float64
'''

回答by Vinit Dawane

# Developing a loop to identify and remove columns where more than 50% of the values are missing#

 i = 0

 count_of_columns_removed = 0

 a = np.array([50,60,70,80,90,100])

 percent_NA = 0

for i in app2.columns:

    percent_NA = round(100*(app2[i].isnull().sum()/len(app2.index)),2)     
    # Replace app2 with relevant name

    if percent_NA >= a.all():
        print(i)
        app2 = app2.drop(columns=i)
        count_of_columns_removed += 1

print(count_of_columns_removed)