我有以下索引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添加到上面的例子中?


当前回答

首先创建一个包含相关数据的python的list_of_e。 用这个: Df ['e'] = list_of_e

其他回答

要在数据帧的给定位置(0 <= loc <=列的数量)插入一个新列,只需使用datafframe .insert:

DataFrame.insert(loc, column, value)

因此,如果你想在一个名为df的数据帧的末尾添加列e,你可以使用:

e = [-0.335485, -1.166658, -0.385571]    
DataFrame.insert(loc=len(df.columns), column='e', value=e)

value可以是一个Series,一个整数(在这种情况下,所有单元格都被这个值填充),或者一个类似数组的结构

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.insert.html

我得到了可怕的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.

但有一点需要注意,如果你这样做了

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

这实际上是df1.index上的左连接。因此,如果您希望具有外部连接效果,我的解决方案可能并不完美,即创建一个包含所有数据的索引值的数据框架,然后使用上面的代码。例如,

data = pd.DataFrame(index=all_possible_values)
df1['e'] = Series(np.random.randn(sLength), index=df1.index)

让我补充一下,就像hum3一样,.loc没有解决SettingWithCopyWarning,我不得不求助于df.insert()。在我的例子中,假阳性是由“假”链索引dict['a']['e']生成的,其中'e'是新列,dict['a']是来自字典的数据框架。

还请注意,如果您知道自己在做什么,您可以使用切换警告 pd.options.mode。chained_assignment =无 然后用这里给出的另一个解。

这是向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()