[Python] reshape a pandas dataframe

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    suppose a dataframe like this one:

    df = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]], columns = ['A', 'B', 'A1', 'B1'])


    [​IMG]

    I would like to have a dataframe which looks like:

    [​IMG]

    what does not work:

    new_rows = int(df.shape[1]/2) * df.shape[0]
    new_cols = 2
    df.values.reshape(new_rows, new_cols, order='F')


    of course I could loop over the data and make a new list of list but there must be a better way. Any ideas ?

    Continue reading...

Compartilhe esta Página