Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何添加新列作为第一列?_Python_Pandas_Dataframe - Fatal编程技术网

Python 如何添加新列作为第一列?

Python 如何添加新列作为第一列?,python,pandas,dataframe,Python,Pandas,Dataframe,我编写了以下代码以向pandas数据帧添加一个新列: 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 \

我编写了以下代码以向pandas数据帧添加一个新列:

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”添加为第一列? 此外,上述给定代码给出了以下警告:

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
如果希望列包含所有0,请使用并利用:

validnew.insert(0, 'const', 0)