1

I wrote the following code to add a new column to the pandas DataFrame:

validnew=validation[features]
validnew['const'] = pd.Series([0 for x in range(len(validation.index))], index=validation.index)

print validnew.head()

       season  holiday  workingday  weather   temp   atemp  humidity  \
10341       4        0           1        2  12.30  15.150        61   
10342       4        0           1        2  13.12  16.665        57   
10343       4        0           1        1  13.94  17.425        53   
10344       4        0           1        1  13.94  16.665        53   
10345       4        0           1        1  15.58  19.695        43   

       windspeed  year  month  weekday  hour  const  
10341    11.0014  2012     11        4     7      0  
10342     8.9981  2012     11        4     8      0  
10343     7.0015  2012     11        4     9      0  
10344    12.9980  2012     11        4    10      0  
10345    15.0013  2012     11        4    11      0 

How can I add column 'const' as the first column? Also, the above-given code gives this warning:

C:/Tests/lreg.py:119: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

1 Answers1

1

Use insert and make use of broadcasting if you want a column of all 0s:

validnew.insert(0, 'const', 0) 
Community
  • 1
  • 1
ely
  • 74,674
  • 34
  • 147
  • 228