Filter Empty Rows in Pandas | Python Guide

To filter rows with certain columns that are empty, the task can be accomplished using the pandas library. Here is an example code:

import pandas as pd

# 创建一个示例数据集
data = {'A': [1, 2, 3, None],
        'B': [None, 5, 6, 7],
        'C': [9, 10, None, 12]}
df = pd.DataFrame(data)

# 筛选'A'列或'B'列为空值的行
filtered_df = df[df['A'].isnull() | df['B'].isnull()]

print(filtered_df)

In the example above, we created a sample dataset and filtered rows with null values in either the ‘A’ column or ‘B’ column using the isnull() method. The results were stored in filtered_df, and the filtered dataset was then printed.

bannerAds