简体   繁体   English

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

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

I have a dataframe called df that looks like this:我有一个名为 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

I used df.setindex('Date') to make the date the index.我使用 df.setindex('Date') 将日期设为索引。 I have 3 questions.我有 3 个问题。

1) How do I display the number of days that had a reading greater than 13 in the entire data frame not just in a single column? 1)如何在整个数据框中显示读数大于 13 的天数,而不仅仅是在单个列中?

I tried df.[(df.Reading1:df.Reading4>13)].shape[0] but obviously the syntax is wrong.我试过 df.[(df.Reading1:df.Reading4>13)].shape[0] 但显然语法错误。

2) How do I display the values that happened on 2000-05-03 for columns Readings 1, 3, and 4? 2) 如何显示 2000 年 5 月 3 日发生的读数 1、3 和 4 列的值?

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

but i got the error "None of the Index(['20000503'],dtype='object')] are in the [index]"但我收到错误“没有索引(['20000503'],dtype='object')] 在 [index] 中”

3) How do find do I display the dates for which the values for the column Readings 1 are twice as much as those in column Readings 2? 3) 如何查找显示读数 1 列的值是读数 2 列值两倍的日期? And how do I display those values (the ones in Reading 1 that are twice as big) as well?以及如何显示这些值(读数 1 中的两倍大)?

I have no idea where to even start this one.我什至不知道从哪里开始。

Try this:尝试这个:

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.

相关问题 如何根据条件将行添加到具有某些行默认值的现有数据框中? - How do you add rows to an existing data frame with default values for certain rows based on condition? 如何根据条件更改python数据框中的值(即列表)? - How to change values (that are lists) in python Data Frame based on condition? 如何根据条件对多索引数据框中的列值进行计数 - How to count column values in multiindex data frame based on condition 如何根据条件在熊猫数据框的多列上分配值 - How to assign values on multiple columns of a pandas data frame based on condition 如何根据数据框中的值删除数据框中的某些条目 - How to remove certain entries in a data frame based on values within the dataframe 如何根据范围表中的值从数据框中选择行 - How to select rows from a data frame based on the values in a table of ranges 在其他列中的某些条件下替换数据框中的项值 - Replacing item values in a data frame on certain condition in other columns 在存在某些条件的情况下,如何根据该数据帧的列值随机选择该数据帧的记录? - How can randomly select records of a data frame based on values of a column of that data frame, while there are some conditions as well? 如何根据条件将 append 1 个数据帧与另一个数据帧 - How to append 1 data frame with another based on condition 如何在数据框中选择一定数量的行? - How to select certain number of rows in data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM