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

问题描述 投票:0回答:1

我有一个名为df的数据框,它看起来像这样。

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-05-03发生在列Readings1、3、4的值?

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

但我得到的错误是 "None of the Index(['20000503'],dtype='object')]are in the [index]"。

3)如何找到显示读数1列的值是读数2列的两倍的日期?我又如何将这些值(读数1中的值是两倍大的)也显示出来呢?

我根本不知道该从哪里下手。

python pandas dataframe indexing shapes
1个回答
0
投票

试试这个。

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.
© www.soinside.com 2019 - 2024. All rights reserved.