+2 votes
in Programming Languages by (74.2k points)
I want to add a new column to an existing dataframe. How can I add?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

If you want to add the new column as the last column, you can use the new column name or the column number as an index for the DataFrame to add values to the new column.

dataframe['new_column_name'] = values

or

dataframe[new_column_number] = values

Examples:

Using Column Name

>>> import pandas as pd
>>> a={'A':[11,12,13], 'B':[21,22,23],'C':[31,32,33]}
>>> df=pd.DataFrame(a)
>>> df
    A   B   C
0  11  21  31
1  12  22  32
2  13  23  33
>>> D=[41,42,43]
>>> df['D']=D
>>> df
    A   B   C   D
0  11  21  31  41
1  12  22  32  42
2  13  23  33  43


Using Column Number

>>> import numpy as np
>>> b=np.array([[11,12,13], [21,22,23],[31,32,33]])
>>> b
array([[11, 12, 13],
       [21, 22, 23],
       [31, 32, 33]])
>>> df1=pd.DataFrame(b)
>>> df1
    0   1   2
0  11  12  13
1  21  22  23
2  31  32  33
>>> df1[3]=D
>>> df1
    0   1   2   3
0  11  12  13  41
1  21  22  23  42
2  31  32  33  43

If you want to add the new column at a particular position, you can use the insert() function.

Examples:

To insert a new column 'E' with values D at position 0:

>>> df.insert(0,'E',D)
>>> df
    E   A   B   C   D
0  41  11  21  31  41
1  42  12  22  32  42
2  43  13  23  33  43


To insert a new column 'F' with values D at position 2:

>>> df.insert(2,'F',D)
>>> df
    E   A   F   B   C   D
0  41  11  41  21  31  41
1  42  12  42  22  32  42
2  43  13  43  23  33  43


...