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.py354 chars14 lines
Output:
main.py11 chars2 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.
gistlibby LogSnag