繁体   English   中英

如何根据数据框中的条件 select 某些值?

[英]How to select certain values based on a condition in a data frame?

我有一个名为 df 的 dataframe 看起来像这样:

Date        Reading1 Reading2 Reading3 Reading4
2000-05-01     15        13        14       11
2000-05-02     15        14        18        9
2000-05-03     14        12        15        8
2000-05-04     17        11        16       13

我使用 df.setindex('Date') 将日期设为索引。 我有 3 个问题。

1)如何在整个数据框中显示读数大于 13 的天数,而不仅仅是在单个列中?

我试过 df.[(df.Reading1:df.Reading4>13)].shape[0] 但显然语法错误。

2) 如何显示 2000 年 5 月 3 日发生的读数 1、3 和 4 列的值?

我试过 df.loc[["20000503"],["Reading1","Reading3,"Reading4"]]

但我收到错误“没有索引(['20000503'],dtype='object')] 在 [index] 中”

3) 如何查找显示读数 1 列的值是读数 2 列值两倍的日期? 以及如何显示这些值(读数 1 中的两倍大)?

我什至不知道从哪里开始。

尝试这个:

1. (df > 13).any(axis=1).sum()
Create a boolean dataframe then check to see if any value is True along the row and sum rows to get number of days.

2. df.loc['2000-05-03', ['Reading1', 'Reading3', 'Reading4']]
Use partial string indexing on DatetimeIndex to get a day, then column filtering with a list of column header.

3. df.loc[df['Reading1']  > (df['Reading2'] * 2)].index
   df.loc[df['Reading1']  > (df['Reading2'] * 2)].to_numpy().tolist()
Create a boolean series to do boolean indexing and get the index to return date.  Next convert the dataframe to numpy array then tolist to get values.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM