orie
orie

Reputation: 571

How to create new columns in a dataframe and assign them all with 0?

Im using this syntax to preallocate columns and assign 0 to all of them:

data['Base'] = 0
data['Base_Chg'] = 0
data['Base_5D_Chg'] = 0
data['Year_Low'] = 0
data['Year_High'] = 0
data['Market_Cap'] = 0
data['PE_Ratio'] = 0
data['SMA_50'] = 0
data['SMA_100'] = 0
data['SMA_200'] = 0
data['RSI'] = 0
data['ADX'] = 0
data['ATR'] = 0
data['STDEV'] = 0

Is there any way of doing the same thing with fewer lines of code?

Using pandas in python.

Thx!

Upvotes: 3

Views: 123

Answers (3)

mechanical_meat
mechanical_meat

Reputation: 169274

You can use keyword argument unpacking with an OrderedDict.

import collections as co

od = co.OrderedDict({'Base':0,'Base_Chg':0,'Base_5D_Chg':0})

data.assign(**od)

Result:

In [18]: data.assign(**od)
Out[18]: 
   a  Base  Base_Chg  Base_5D_Chg
0  1     0         0            0
1  2     0         0            0
2  3     0         0            0

Upvotes: 3

Code Different
Code Different

Reputation: 93151

At the very least, you still have to write out all the new columns' names.

You can use a loop:

columns=['Base', 'Base_Chg', 'Base_5D_Chg', 'Year_Low', 'Year_High', 'Market_Cap', 'PE_Ratio', 'SMA_50', 'SMA_100', 'SMA_200', 'RSI', 'ADX', 'ATR', 'STDEV']
for col in columns:
    df[col] = 0

Or pd.concat:

columns=['Base', 'Base_Chg', 'Base_5D_Chg', 'Year_Low', 'Year_High', 'Market_Cap', 'PE_Ratio', 'SMA_50', 'SMA_100', 'SMA_200', 'RSI', 'ADX', 'ATR', 'STDEV']
new_df = pd.DataFrame(0, columns=columns, index=df.index)
df = pd.concat([df, new_df], axis=1)

Test to see which one is faster for your use case.

Upvotes: 1

Umar.H
Umar.H

Reputation: 23099

assuming your column names are in a list, we create a dictionary with the key as the column name as 0 as the value. We then do a caretersian join onto df1.

cols = ['Base', 
'Base_Chg', 
'Base_5D_Chg', 
'Year_Low', 
'Year_High', 
'Market_Cap', 
'PE_Ratio', 
'SMA_50', 
'SMA_100', 
'SMA_200', 
'RSI', 
'ADX', 
'ATR', 
'STDEV']

df1 = pd.DataFrame({'A' : [0,1,2,3]}) # your original dataframe.

df2 = pd.DataFrame(dict(zip(cols,[0] * len(cols))),index=[0]) 
#new dataframe from list of cols.

df3 = pd.merge(df1.assign(key='key'),df2.assign(key='key'),how='outer').drop('key',axis=1)
#merge of your old dataframe and new.

print(df1)
   A
0  0
1  1
2  2
3  3

print(df2)
   Base  Base_Chg  Base_5D_Chg  Year_Low  Year_High  Market_Cap  PE_Ratio  \
0     0         0            0         0          0           0         0   

   SMA_50  SMA_100  SMA_200  RSI  ADX  ATR  STDEV  
0       0        0        0    0    0    0      0  

print(df3)



   A  Base  Base_Chg  Base_5D_Chg  Year_Low  Year_High  Market_Cap  PE_Ratio  \
0  0     0         0            0         0          0           0         0   
1  1     0         0            0         0          0           0         0   
2  2     0         0            0         0          0           0         0   
3  3     0         0            0         0          0           0         0   

   SMA_50  SMA_100  SMA_200  RSI  ADX  ATR  STDEV  
0       0        0        0    0    0    0      0  
1       0        0        0    0    0    0      0  
2       0        0        0    0    0    0      0  
3       0        0        0    0    0    0      0  

Upvotes: 1

Related Questions