我有以下索引DataFrame命名列和行不连续的数字:

          a         b         c         d
2  0.671399  0.101208 -0.181532  0.241273
3  0.446172 -0.243316  0.051767  1.577318
5  0.614758  0.075793 -0.451460 -0.012493

我想添加一个新列,'e',到现有的数据帧,并不想改变数据帧中的任何东西(即,新列始终具有与DataFrame相同的长度)。

0   -0.335485
1   -1.166658
2   -0.385571
dtype: float64

如何将列e添加到上面的例子中?


当前回答

如果你只需要创建一个新的空列,那么最短的解决方案是:

df.loc[:, 'e'] = pd.Series()

其他回答

我得到了可怕的SettingWithCopyWarning,它没有通过使用iloc语法修复。我的DataFrame是由read_sql从ODBC源创建的。根据上面low - tech的建议,以下方法对我来说是有效的:

df.insert(len(df.columns), 'e', pd.Series(np.random.randn(sLength),  index=df.index))

This worked fine to insert the column at the end. I don't know if it is the most efficient, but I don't like warning messages. I think there is a better solution, but I can't find it, and I think it depends on some aspect of the index. Note. That this only works once and will give an error message if trying to overwrite and existing column. Note As above and from 0.16.0 assign is the best solution. See documentation http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html#pandas.DataFrame.assign Works well for data flow type where you don't overwrite your intermediate values.

直接通过NumPy这样做将是最有效的:

df1['e'] = np.random.randn(sLength)

注意我最初(非常老)的建议是使用map(这要慢得多):

df1['e'] = df1['a'].map(lambda x: np.random.random())

向现有数据帧中添加一个新列'e'

 df1.loc[:,'e'] = Series(np.random.randn(sLength))

在分配新列之前,如果已经索引了数据,则需要对索引进行排序。至少在我的情况下,我必须:

data.set_index(['index_column'], inplace=True)
"if index is unsorted, assignment of a new column will fail"        
data.sort_index(inplace = True)
data.loc['index_value1', 'column_y'] = np.random.randn(data.loc['index_value1', 'column_x'].shape[0])

这是向pandas数据框架添加新列的特殊情况。在这里,我基于数据框架的现有列数据添加了一个新特性/列。

因此,让我们的dataFrame有列'feature_1', 'feature_2', 'probability_score',我们必须根据'probability_score'列中的数据添加一个new_column 'predicted_class'。

我将使用来自python的map()函数,并定义一个我自己的函数,该函数将实现如何给dataFrame中的每一行一个特定的class_label的逻辑。

data = pd.read_csv('data.csv')

def myFunction(x):
   //implement your logic here

   if so and so:
        return a
   return b

variable_1 = data['probability_score']
predicted_class = variable_1.map(myFunction)

data['predicted_class'] = predicted_class

// check dataFrame, new column is included based on an existing column data for each row
data.head()