[Python] In pandas, a groupby followed by boxplot gives KeyError: "None of [Index(['A', 1],...

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    This very simple script gives error KeyError: "None of [Index(['A', 1], dtype='object')] are in the [index]":

    import pandas as pd
    import matplotlib.pyplot as plt
    L1 = ['A','A','A','A','B','B','B','B']
    L2 = [1,1,2,2,1,1,2,2]
    V = [9.8,9.9,10,10.1,19.8,19.9,20,20.1]
    df = pd.DataFrame({'L1':L1,'L2':L2,'V':V})

    print(df)

    df.groupby(['L1','L2']).boxplot(column='V')
    plt.show()


    So my dataframe is:

    L1 L2 V
    0 A 1 9.8
    1 A 1 9.9
    2 A 2 10.0
    3 A 2 10.1
    4 B 1 19.8
    5 B 1 19.9
    6 B 2 20.0
    7 B 2 20.1


    and I would expect a plot with four boxplot showing the values V, the labels of boxplots should be A/1, A/2, B/1, B/2.

    I had a look at How To Solve KeyError: u"None of [Index([..], dtype='object')] are in the [columns]" but I was not able to fix my error, AI tools are not helping me either.

    What am I not understanding?

    Continue reading...

Compartilhe esta Página