[Python] Must have equal len keys and value when setting with an iterable

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have two dataframes as follows:

    leader:

    0 11
    1 8
    2 5
    3 9
    4 8
    5 6
    [6065 rows x 2 columns]


    DatasetLabel:

    0 1 .... 7 8 9 10 11 12
    0 A J .... 1 2 5 NaN NaN NaN
    1 B K .... 3 4 NaN NaN NaN NaN
    [4095 rows x 14 columns]


    The Information dataset column names 0 to 6 are DatasetLabel about data and 7 to 12 are indexes that refer to the first column of leader Dataframe.

    I want to create dataset where instead of the indexes in DatasetLabel dataframe, I have the value of each index from the leader dataframe, which is leader.iloc[index,1].

    How can I do it using python features?

    The output should look like:

    DatasetLabel:

    0 1 .... 7 8 9 10 11 12
    0 A J .... 8 5 6 NaN NaN NaN
    1 B K .... 9 8 NaN NaN NaN NaN


    I have come up with the following, but I get an error:

    for column in DatasetLabel.ix[:, 8:13]:
    DatasetLabel[DatasetLabel[column].notnull()] = leader.iloc[DatasetLabel[DatasetLabel[column].notnull()][column].values, 1]


    Error:

    ValueError: Must have equal len keys and value when setting with an iterable

    Continue reading...

Compartilhe esta Página