简体   繁体   中英

Finding the smallest number not smaller than x for a rolling data of a dataframe in python

Let suppose i have data in rows for a column(O): 1,2,3,4,5,6,7,8,9,10. Its average is 5.5. I need to find the smallest number which is larger than the average 5.5:- ie '6'

Here is what I have tried soo far.

method 1:

df["test1"] = df["O"].shift().rolling(min_periods=1, window=10).apply(lambda x: pd.Series(x).nlargest(5).iloc[-1])

Discarded as that number may not always be the 6th number.

method 2:

great = []
df['test1'] = ''
df["avg"] = df["O"].shift().rolling(min_periods=1, window=10).mean()
for i in range(1, len(df)):
    for j in range(0, 10):
        if(df.loc[i-j, 'O'] > df.loc[i, 'avg']):
            great.append(df.loc[i-j, 'O'])
    df.loc[i, 'test1'] = min(great)

This throws an error:

KeyError: -1

Please help to find the small error in the code as soon as possible.

Thanks, D.

Mask the Series when it is greater than the mean, then sort, then take the first row.

import pandas as pd
df = pd.DataFrame([1,2,3,4,5,6,7,8,9,10], columns=("vals",))
df[df.vals > df.vals.mean()].sort_values("vals").head(1)
# >     vals
#    5  6

Try with

n = 10
output = df.vals.rolling(10).apply(lambda x : x[x>x.mean()].min())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM