如何添加一個新欄作為第一欄?

2024-04-19 17:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了以下代码来向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 

如何将列“const”添加为第一列? 此外,上述代码也会发出此警告:

^{pr2}$

Tags: 代码indataframepandasforindexlenrange