Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding column in pandas dataframe containing the same value

Tags:

python

pandas

I have a pandas dataframe A of size (1500,5) and a dictionary D containing:

D
Out[121]: 
{'newcol1': 'a',
 'newcol2': 2,
 'newcol3': 1}

for each key in the dictionary I would like to create a new column in the dataframe A with the values in the dictionary (same value for all the rows of each column)

at the end A should be of size (1500,8)

Is there a "python" way to do this? thanks!

like image 235
gabboshow Avatar asked Jan 04 '23 15:01

gabboshow


1 Answers

You can use concat with DataFrame constructor:

D = {'newcol1': 'a',
 'newcol2': 2,
 'newcol3': 1}

df = pd.DataFrame({'A':[1,2],
                   'B':[4,5],
                   'C':[7,8]})

print (df)
   A  B  C
0  1  4  7
1  2  5  8

print (pd.concat([df, pd.DataFrame(D, index=df.index)], axis=1))
   A  B  C newcol1  newcol2  newcol3
0  1  4  7       a        2        1
1  2  5  8       a        2        1

Timings:

D = {'newcol1': 'a',
 'newcol2': 2,
 'newcol3': 1}

df = pd.DataFrame(np.random.rand(10000000, 5), columns=list('abcde'))

In [37]: %timeit pd.concat([df, pd.DataFrame(D, index=df.index)], axis=1)
The slowest run took 18.06 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 875 ms per loop

In [38]: %timeit df.assign(**D)
1 loop, best of 3: 1.22 s per loop
like image 71
jezrael Avatar answered Jan 13 '23 10:01

jezrael