Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回数据帧中信息的整行_Python_Pandas - Fatal编程技术网

Python 返回数据帧中信息的整行

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

我有一个数据帧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 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]