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!
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.