[Python] Issue with Pandas: Handling Dictionaries with Mixed Lists and Ranges

Discussão em 'Python' iniciado por Stack, Dezembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working with a dictionary that contains a mix of lists and ranges (which are processed into lists), but when I try to convert it into a pandas DataFrame, I get the error:

    ValueError: All arrays must be of the same length.


    I am also not able to query and find the values I want due to the same reason.

    The below pasted code is a small section of my code and I am running into the above mentioned error. I also get IndexError: single positional indexer is out-of-bounds.

    ScaleX_Adjustment = {
    'X-Raw': [range(0, 34), list(range(34, 61)), range(61, 124), list(range(124, 179))],
    '1-8B': [+20, +20, +20, +20, +19, +18, +17, +17, +16, +15, +14, +13, +13, +12, +11, +10, +9, +9, +8, +7, +6, +5,
    +5, +4, +3, +2, +1, +1, 0, -1, -1, -1, -2, -2, -3, -3, -3, -4, -4, -5, -5, -5, -6, -6, -7, -7, -7, -8,
    -8, -9, -9 - 9, -10, -10, -11, -11, -11, -12, -12, -13, -13, -13, -14, -14, -15, -15, -15, -16, -16,
    -17, -17, -17, -18, -18, -19, -19, -19, -20, -20, -20, -20, -20, -20, -20],
    'S-PP': [+10, +10, +10, +10, +10, +10, 9, 9, 9, 8, 8, 7, 7, 7, 6, 6, 5, 5, 5, 4, 4, 3, 3, 3, 2, 2, 1, 1, 0, -1,
    -1, -1, -2, -2, -2, -2, -2, -3, -3, -3, -3, -3, -4, -4, -4, -4, -4, -5, -5, -5, -5, -5, -6, -6, -6, -6,
    -6, -7, -7, -7, -7, -7, -8, -8, -8, -8, -8, -9, -9, -9, -9, -9, -10, -10, -10, -10, -10, -11, -11, -11,
    -11, -11, -11, -11]
    }
    ScaleX_Adjustment_DF = pd.DataFrame(ScaleX_Adjustment)
    ScaleX_1to8B = ScaleX_Adjustment_DF.loc[ScaleX_Adjustment_DF['X-Raw'] == ScaleX_Raw, '1-8B'].iloc[0]
    ScaleX_StoPP = ScaleX_Adjustment_DF.loc[ScaleX_Adjustment_DF['X-Raw'] == ScaleX_Raw, 'S-PP'].iloc[0]


    I need to retain the original structure of my dictionary (with lists and ranges) and find a solution for converting it into a DataFrame without encountering this error. Any suggestions or workarounds?

    One thing I tried to do was expand all the lists and ranges and remove the structure I gave but that will be highly susceptible to low reliability and there should be a better workaround I feel.

    Continue reading...

Compartilhe esta Página