[Python] How do I repeat one Dataframe to match the length of another DataFrame

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I want to combine two DataFrames of unequal length to a new DataFrame with the size of the larger one. Now, specifically, I want to pad the values of the shorter array by repeating it until it is large enough.

    I know this is possible for lists using itertools.cycle as follows:

    from itertools import cycle

    x = range(7)
    y = range(43)

    combined = zip(cycle(x), y)


    Now I want to do the same for DataFrames:

    import pandas as pd

    df1 = pd.DataFrame(...) # length 7
    df2 = pd.DataFrame(...) # length 43

    df_comb = pd.concat([cycle(df1),df2], axis=1)


    Of course this doesn't work, but I don't know if there is an option to do this or to just manually repeat the array.

    Continue reading...

Compartilhe esta Página