How do you get the list of columns that have null values in a dataframes?
Question: How do you get the list of columns that have null values in a dataframes?
To get the list of columns that have null values in a Pandas DataFrame, you can use the `isnull()` function along with the `any()` function to check for null values in each column. Here's an example:
```python
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, None, 4], 'B': [None, 6, 7, 8], 'C': [9, None, 11, 12]})
# check for null values in each column
null_cols = df.isnull().any()
# get the list of column names with null values
null_cols_list = null_cols[null_cols == True].index.tolist()
# print the list of columns with null values
print(null_cols_list)
```
In this example, the `isnull()` function checks for null values in each column of the DataFrame `df`. The `any()` function is then used to determine which columns have at least one null value, and the resulting boolean Series is stored in `null_cols`. The list of column names with null values is then obtained by filtering the `null_cols` Series to only include True values and converting the resulting index to a list using `tolist()`. Finally, the list of columns with null values is printed using `print()`.
0 Komentar
Post a Comment