簡體   English   中英

比較兩個數據框並找到空值的數量

[英]Compare two data frames and find number of nulls

我有個問題。 我有名為“df”的數據框 1:

在此處輸入圖片說明

我有名為“dfP1”的數據框 2:

在此處輸入圖片說明

我想比較“dfP1”中的列“Campo a Validar”中存在的唯一行與“df”中的列,如果存在計算匹配列中空值數量的巧合。 然后空值的數量將在數據幀 df 中插入到新的列名稱“Numeros_de_nulos”中,但僅在第 0 行(索引 0)中。

這是嘗試過的東西:

    #Validacion de Regla 1
if pd.isnull(df["Nº Línea Cliente"]).values.ravel().sum() > 0:
    nulos = pd.isnull(df["Nº Línea Cliente"]).values.ravel().sum()
    print("Hay {} valores nulos".format(nulos))
    dfP1['Numeros_de_Nulos'] = None

else:
    print ("No hay valores nulos")
dfP1.head()

我想我可能有一個答案。

# Count number of NULL values in column 'Nº Línea Cliente'
nulos = df['Nº Línea Cliente'].isnull().sum()

# If nulos is greater than zero
if nulos > 0:
    # Create a column of nulls
    dfP1['Numeros_de_Nulos'] = None
    # dfP1['Numeros_de_Nulos'] = 0
    # dfP1['Numeros_de_Nulos'] = np.NaN

    # Use DataFrame.loc[<index>, <column name>] to set a new value
    dfP1.loc[0, 'Numeros_de_Nulos'] = nulos

輸出:

   ID_val  Tipo_Validacion     Campo_a_Validar Numeros_de_Nulos
0       1                1    Nº Línea Cliente                1
1       2                2    Nº Línea Cliente             None
2       3                3    Nº Línea Cliente             None
3       4                4    Nº Línea Cliente             None
4       5                1  TIPO DE GARANTIA 1             None

* 更多關於pandas.DataFrame.loc() 在這里

確定您需要什么有點挑戰性,但這可能接近您的理想解決方案。

import pandas as pd
from numpy import NaN

# Assuming that these dictionaries accurately reflect
# your DataFrames's contents, then the 
# following might work:

_df = {
    "c1":  [1.0, 3.0, 5.0, 7.0],
    "c2":  [1.0, 3.0, 5.0, 7.0],
    "c3":  [1.0, 3.0, 5.0, 7.0],
    "c4":  [1.0, 3.0, 5.0, 7.0],
    "Nº Línea Cliente": [
        "Hay algo",
        "Hay algo",
        "Hay algo",
        NaN],
    "c6":  [1.0, 3.0, 5.0, 7.0],
    "c7":  [1.0, 3.0, 5.0, 7.0],
    "c8":  [1.0, 3.0, 5.0, 7.0],
    "c9":  [1.0, 3.0, 5.0, 7.0],
    "c10": [1.0, 3.0, 5.0, 7.0],
}

Campo_a_Validar = [
        "Nº Línea Cliente"
        for campo in range(4)]
Campo_a_Validar.append("TIPO DE GARANTIA 1")

_dfP1 = {
    "ID_Val": [1,2,3,4,5],
    "Tipo_Validación": [1, 2, 3, 4, 1],
    "Campo_a_Validar": Campo_a_Validar,
}

# Initializing the DataFrames

df = pd.DataFrame(_df)
dfP1 = pd.DataFrame(_dfP1)

def analizar_para_nulos(_df_, _dfP1_):
    try:
        contar_nulos  = lambda DF, ColName: DF.groupby([ColName])[ColName].nunique()
        nulos_de_df   = contar_nulos(_df_, "Nº Línea Cliente")
        nulos_de_dfP1 = contar_nulos(_dfP1_, "Campo_a_Validar") 
        assert(
            nulos_de_df.values[0] == nulos_de_dfP1.values[0]
        )
        num_nulos = nulos_de_df
        return num_nulos.values[0]
    except AssertionError:
        return 0

# Check whether the number of unique rows is
# equal to the number of unique rows in
# the other table

is_coincidence = analizar_para_nulos(df, dfP1)

if is_coincidence:
    base = [is_coincidence]
    base.extend([""
        for position in range(len(df.c1) - 1)])
    num_columns = len(df.T)
    df.insert(
        loc=num_columns,
        column="Numeros_de_Nulos",
        value=base
    )
    print(df)
else:
    print(df)

輸出:

    c1   c2   c3   c4 Nº Línea Cliente   c6   c7   c8   c9  c10 Numeros_de_Nulos
0  1.0  1.0  1.0  1.0         Hay algo  1.0  1.0  1.0  1.0  1.0                1
1  3.0  3.0  3.0  3.0         Hay algo  3.0  3.0  3.0  3.0  3.0                 
2  5.0  5.0  5.0  5.0         Hay algo  5.0  5.0  5.0  5.0  5.0                 
3  7.0  7.0  7.0  7.0              NaN  7.0  7.0  7.0  7.0  7.0                 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM