An efficient way to create new columns in pandas

Is there a more efficient way to create multiple new columns in pandas dataframe df

, initialized to zero than:

for col in add_cols:
   df.loc[:, col] = 0

      

+2


source to share


4 answers


UPDATE: using @jeff method , but dynamically do:

In [208]: add_cols = list('xyz')

In [209]: df.assign(**{i:0 for i in add_cols})
Out[209]:
   a  b  c  x  y  z
0  4  8  6  0  0  0
1  3  7  0  0  0  0
2  4  0  1  0  0  0
3  5  4  5  0  0  0
4  1  3  0  0  0  0

      

OLD answer:

Another method:



df[add_cols] = pd.DataFrame(0, index=df.index, columns=add_cols)

      

Demo:

In [343]: df = pd.DataFrame(np.random.randint(0, 10, (5,3)), columns=list('abc'))

In [344]: add_cols = list('xyz')

In [345]: add_cols
Out[345]: ['x', 'y', 'z']

In [346]: df
Out[346]:
   a  b  c
0  4  9  0
1  1  1  1
2  8  8  1
3  0  1  4
4  8  5  6

In [347]: df[add_cols] = pd.DataFrame(0, index=df.index, columns=add_cols)

In [348]: df
Out[348]:
   a  b  c  x  y  z
0  4  9  0  0  0  0
1  1  1  1  0  0  0
2  8  8  1  0  0  0
3  0  1  4  0  0  0
4  8  5  6  0  0  0

      

+2


source


In [13]: df = pd.DataFrame(np.random.randint(0, 10, (5,3)), columns=list('abc'))

In [14]: df
Out[14]: 
   a  b  c
0  7  2  3
1  7  0  7
2  5  1  5
3  9  1  4
4  2  1  4

In [15]: df.assign(x=0, y=0, z=0)
Out[15]: 
   a  b  c  x  y  z
0  7  2  3  0  0  0
1  7  0  7  0  0  0
2  5  1  5  0  0  0
3  9  1  4  0  0  0
4  2  1  4  0  0  0

      



+2


source


Here's a hack:

[df.insert(0, col, 0) for col in add_cols]

      

+1


source


You can process the DataFrame with the dict type syntax:

for col in add_cols:
    df[col] = 0

      

+1


source







All Articles