簡體   English   中英

如何遍歷 dataframe 到 select 滿足條件的行,包括在 python 中的索引

[英]How to Iterate through a dataframe to select rows that satisfies a condition including their index in python

我在根據代碼中定義的條件調用行及其相應索引時遇到問題。 我已經嘗試了這里找到的所有示例,但沒有一個與我遇到的問題完全相同。 數據集如下所示:

import pandas as pd
import datetime
import numpy as np
from matplotlib import pyplot as plt

df = pd.read_csv('Datasets.csv')
print(df)

Posted_Time Label
07/01/2018  13.01
14/01/2018  9.80
21/01/2018  9.08
28/01/2018  8.64
04/02/2018  8.78
11/02/2018  7.27
18/02/2018  7.16
25/02/2018  7.09
04/03/2018  5.85
11/03/2018  8.71

Posted_Time是索引列,它是日期時間格式。 我想要 select 所有超過為Label列定義的設置閾值的行,如下所示。

df2 = df.ewm(span=4, adjust=False).mean()
mean = df2['Label'].mean().astype(float)
std = df2['Label'].std().astype(float)
thres = std+mean

i = 0
control = True
for record in df2['Label']:
    if record > thres:
        print(i, 'in position!', i)
        control = False
    i += 1
if control == True:
    print('All points are within control limits.')

當我運行我的代碼時,它返回 dataframe 中的記錄 position 而不是實際記錄和相應的索引( Posted_Time )。 這是我得到的結果。

0 in position! 0
1 in position! 1
19 in position! 19
23 in position! 23

我究竟做錯了什么? 有人可以幫助糾正我的迭代代碼以達到下面的預期結果嗎?

07/01/2018  13.01 in position 0 
14/01/2018  9.80
21/01/2018  9.08
04/02/2018  8.78
11/03/2018  8.71

謝謝

我認為您只需要更改您的打印聲明。 你有 print(i, 'in position,'。i) 我認為你需要將第一個“i”更改為“record”,如果這是你想要打印的。

你可以試試這個:

df2['position'] = range(0, len(df2))
for idx, row in df2.iterrows():
    if row['Label'] > thres:
        print('{} in position {} for date: {}'.format(row['Label'], str(row['position']), idx)

這將遍歷 df2 的每一行,檢查Label值是否大於閾值,如果滿足該條件,則打印一條消息。 因為您使用Posted_time作為索引,所以任何給定行的Posted_time值將在迭代期間保存在idx中。 如果Posted_time是日期時間,則必須先將其轉換為str才能打印。 由於您沒有數字索引,我們需要在 DataFrame 中添加一列,它可以有效地告訴我們我們所在的行。 這是df2['position']=range(0, len(df2))行的目的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM