【问题标题】:Getting only True values and their respective indices from a Pandas series从 Pandas 系列中仅获取 True 值及其各自的索引
【发布时间】:2019-08-01 20:55:20
【问题描述】:

我有一个看起来像这样的熊猫系列,是在查询数据帧时提取的。

t_loc=
312 False
231 True
324 True
286 False
123 False
340 True

我只想要具有“真”布尔值的索引。

我试过 t_loc.index 给了我所有的索引。 t_loc['True'] 或 t_loc[True] 都是徒劳的。需要帮忙。 另外,如果为真,我需要用一个数字更新这些位置。给定位置编号,如何更新数据框中的列?

所需的 O/P: [231,324,340]

需要更新,例如。 df[col1] @ 231.. 是 df[col1].loc[231] 吗?如何指定多个位置?我们可以传递整个列表吗,因为我只需要为所有位置更新一个值吗?

【问题讨论】:

  • t_loc.index[t_loc]
  • 太棒了!!工作.. 谢谢.. 对于列更新,我可以使用 df['col1'].loc[a] = 40 (我要更新的值)给定 a= t_loc.index[t_loc]
  • 一切正常.. !!
  • 其实df.loc[t_loc, 'col1'] = 40就可以了。

标签: pandas boolean series


【解决方案1】:

你也可以试试这个

t_loc.astype(int).index[t_loc == 1]

【讨论】:

    【解决方案2】:

    这实际上也有效:

    t_loc.index[t_loc == True/False]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-09
      • 2020-11-23
      • 2014-03-07
      • 2014-02-06
      • 2018-05-22
      • 2020-03-17
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多