【问题标题】:How to set values in a data frame based on index [duplicate]如何根据索引在数据框中设置值[重复]
【发布时间】:2019-01-15 12:04:36
【问题描述】:

这是我的数据

     customer_id       feature_1        feature_2      feature_3
0    1                 78               73             63
1    2                 79               71             66
2    2                 82               76             69
3    3                 43               32             53
4    3                 63               42             54

我想一一标记数据框。例如,对于 index = 3,目标是 Bad

     customer_id       feature_1        feature_2      feature_3     target
0    1                 78               73             63
1    2                 79               71             66
2    2                 82               76             69
3    3                 43               32             53            bad
4    3                 63               42             54

基本上,我是用我的注释特辑来一一拉出的过程

最好的问候

【问题讨论】:

  • 有重复吗?如果是,为什么没有人标记这个

标签: python pandas dataframe


【解决方案1】:

使用set_value函数

syntax format is: `DataFrame.set_value(index, col, value, takeable=False)[source]`

所以对于你的问题,答案是

df.set_value(3, 'target', 'bad')

【讨论】:

    【解决方案2】:

    或者,您可以先添加空列,然后用所需值填充单元格

    df['target'] = ''
    df['target'].iloc[3] = 'bad'
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 2021-12-16
      • 2018-11-30
      • 2019-02-16
      • 2011-09-20
      • 2017-08-22
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多