print columns that have a missing value in pandas dataframe in python

To print the columns that have missing values in a Pandas DataFrame in Python, you can use the isnull() function along with the sum() function.

Here's an example:

main.py
import pandas as pd

# Create a sample DataFrame
data = {'A': [1, 2, None, 4, 5],
        'B': [None, 2, 3, 4, 5],
        'C': [1, 2, 3, 4, 5]}
df = pd.DataFrame(data)

# Find the columns with missing values
columns_with_missing_values = df.columns[df.isnull().any()].tolist()

# Print the columns with missing values
print(columns_with_missing_values)
354 chars
14 lines

Output:

main.py
['A', 'B']
11 chars
2 lines

In this example, we first create a sample DataFrame with missing values. Then, we use the isnull() function to create a boolean DataFrame where True denotes a missing value. Next, we apply the any() function along the columns axis (axis=1) to identify which columns contain any missing values. Finally, we use the tolist() function to convert the resulting column names into a list and print it.

Note: The isna() function can also be used instead of isnull() as they are interchangeable.

By running this code, you will see the columns 'A' and 'B' printed, which are the columns that have missing values in the DataFrame.

related categories

gistlibby LogSnag