Python 返回数据帧中信息的整行
我有一个数据帧df,看起来像:Python 返回数据帧中信息的整行,python,pandas,Python,Pandas,我有一个数据帧df,看起来像: Date Last Price 0 29/11/2016 56.87 1 28/11/2016 57.32 2 27/11/2016 55.56 3 22/11/2016 58.66 4 21/11/2016 57.98 5 20/11/2016 790.22 我使用以下方法识别时间序列中的异常值: import pan
Date Last Price
0 29/11/2016 56.87
1 28/11/2016 57.32
2 27/11/2016 55.56
3 22/11/2016 58.66
4 21/11/2016 57.98
5 20/11/2016 790.22
我使用以下方法识别时间序列中的异常值:
import pandas as pd
from numpy import mean
from numpy import std
def outliers(df):
data_mean, data_std = mean(df['Last Price']), std(df['Last Price'])
print('data_mean ',data_mean)
print('data_std ',data_std)
cut_off = data_std * 3
lower, upper = data_mean - cut_off, data_mean + cut_off
outliers = [x for x in df['Last Price'] if x < lower or x > upper]
如何将df中的整行返回到列表或数据帧中?因此,我期望的输出是:
20/11/2016 790.22
您可以使用df.iloc[idx].\u列表中idx对应于您的行号。您可以这样做,而不是使用for循环来过滤数据帧:
is_outlier1 = df['Last Price'] < lower
is_outlier2 = df['Last Price'] > upper
outliers = df[is_outlier1][is_outlier2]
这将以数据帧的形式返回异常值
is_outlier1 = df['Last Price'] < lower
is_outlier2 = df['Last Price'] > upper
outliers = df[is_outlier1][is_outlier2]