要遍历 DataFrame 中的每一行并计算原始列中 NaN 值的数量和额外列中非 NaN 值的数量,您可以执行以下操作:
import pandas as pd
# Define the dataframe
df = pd.DataFrame(
{
"Heading 1": [np.nan, np.nan, 5, 5, np.nan, np.nan],
"Heading 2": [34, np.nan, np.nan, 7, np.nan, np.nan],
"Unnamed: 1": [24, 44, np.nan, np.nan, 13, np.nan],
"Unnamed: 2": [np.nan, np.nan, np.nan, np.nan, 77, 18]
}
)
# Define the original columns and the extra columns
original_cols = ["Heading 1", "Heading 2"]
extra_cols = ["Unnamed: 1", "Unnamed: 2"]
# Create a dictionary to store the counts
counts = {}
# Iterate through each row in the DataFrame
for index, row in df.iterrows():
# Count the number of NaN values in the original columns
original_nan_count = sum(row[col].isna() for col in original_cols)
# Count the number of non-NaN values in the extra columns
extra_non_nan_count = sum(not row[col].isna() for col in extra_cols)
# Add the counts to the dictionary
counts[index] = [original_nan_count, extra_non_nan_count]
# Print the dictionary of counts
print(counts)
这将遍历 DataFrame 中的每一行,计算原始列中 NaN 值的数量和额外列中非 NaN 值的数量,并将计数存储在字典中,其中键是行索引和值是包含计数的列表。生成的字典将如下所示:
{0: [1, 1],
1: [2, 1],
2: [1, 0],
3: [0, 0],
4: [2, 2],
5: [2, 1]}