在pandas dataframe中将元素添加到数组的末尾

时间:2017-04-21 17:02:35

标签: python arrays list pandas dataframe

  Feature1                                      Feature2
  [0.0450819672, 0.0450819672, 0.0081967213]    8.236613
  [0.0551181102, 0.0551181102, 0.0078740157]    7.954549

我想在Feature1数组的末尾添加一个float2类型的Feature2。 这就是我尝试实现它的方法,但Feature2_1似乎是一个空列。

  df_2['Feature2_1'] = ''
  for index, row in df.iterrows():
    print row[df['Feature2']]
    value = row[df_2['Feature2']]
    df_2.set_value(index,'Feature2_1',value)

任何建议都会受到高度赞赏!!

1 个答案:

答案 0 :(得分:4)

试试这个:

In [99]: df
Out[99]:
                                     Feature1  Feature2
0  [0.0450819672, 0.0450819672, 0.0081967213]  8.236613
1  [0.0551181102, 0.0551181102, 0.0078740157]  7.954549

In [100]: df.Feature1 += df.Feature2.apply(lambda x: [x])

In [101]: df
Out[101]:
                                               Feature1  Feature2
0  [0.0450819672, 0.0450819672, 0.0081967213, 8.236613]  8.236613
1  [0.0551181102, 0.0551181102, 0.0078740157, 7.954549]  7.954549