我有一个看起来像这样的数据框(但将来可能会有更多列):
Serial No 2015 2016 2017 2018
1 20 30 25 35
2 22 35 47 30
我希望选择 4 列中至少有 3 列的值高于该列平均值的行。我将不胜感激更灵活的答案,例如可以轻松编辑以使其成为 4 列中的 2 列或 6 列中的 4 列,或者可能高于平均水平至少 5%
如果我想要所有列,我可以简单地编写类似 df.2015>df.2015.mean() & df.2016>df.2016.mean() 等的内容,但是如果我想至少看到 y 列中的 x 列,如果没有极其复杂的逻辑,这种方法将无法工作。
是否有任何相对简单的方法可以做到这一点,或者这真的像我担心的那样困难吗?谢谢。
请您参考如下方法:
试试这个:
import pandas as pd
import numpy as np
df = pd.DataFrame(data=np.random.random(size=(10, 3)), columns=['a', 'b', 'c'])
df[np.sum(df > df.mean(), axis=1) > 2] # 2 is the argument that
# defines the number of values that
# should be above the mean
