[Python] Insert new column (of blanks) into an existing dataframe

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

  1. Stack

    Stack Membro Participativo

    I have an existing dataframe:

    data = [[5011025, 234], [5012025, 937], [5013025, 625]]
    df = pd.DataFrame(data)


    output:

    0 1
    0 5011025 234
    1 5012025 937
    2 5013025 625


    What I need to do is insert a new column at 0 (the same # of rows) that contains 3 spaces. Recreating the dataframe, from scratch, it would be something like this:

    data = [[' ',5011025, 234], [' ',5012025, 937], [' ',5013025, 625]]
    df = pd.DataFrame(data)


    desired output:

    0 1 2
    0 5011025 234
    1 5012025 937
    2 5013025 625


    What is the best way to insert() this new column into an existing dataframe, that may be hundreds of rows? Ultimately, i'm trying to figure out how to write a function that will shift all columns of a dataframe x number of spaces to the right.

    Continue reading...

Compartilhe esta Página