Insert a column to a pandas dataframe

前端 未结 2 534
盖世英雄少女心
盖世英雄少女心 2021-01-13 07:00

Scenario: I have a code that reads data from excel worksheets into dataframes, merges into one dataframe, and perform some cleaning procedures.

2条回答
  •  -上瘾入骨i
    2021-01-13 07:53

    You could also append it to the end of the dataframe (the order of the columns generally shouldn't matter except for presentation purposes).

    fnl = fnl.assign(N=15)
    

    This returns a copy of the dataframe with the new column N added with values equal to 15.

    Because the index doesn't matter in this case, it should be more efficient to append via:

    fnl['N'] = 15
    

提交回复
热议问题