【发布时间】:2017-11-17 18:33:16
【问题描述】:
我有以下数据框。 (这不一定是数据框;numpy 数组 df.values 的解决方案也足够了)
np.random.seed(42)
df = pd.DataFrame(np.random.random((10,2)),columns=['a', 'b'])
df
a b
0 0.374540 0.950714
1 0.731994 0.598658
2 0.156019 0.155995
3 0.058084 0.866176
4 0.601115 0.708073
5 0.020584 0.969910
6 0.832443 0.212339
7 0.181825 0.183405
8 0.304242 0.524756
9 0.431945 0.291229
我想包含一个具有以下逻辑值的新列:
真:如果某个特定 a 值之后的任何 b 值大于该部分 a 值
假:否则
预期的输出是: (请参阅下面一些行的解释)
a b c
0 0.374540 0.950714 True
1 0.731994 0.598658 True
2 0.156019 0.155995 True
3 0.058084 0.866176 True <- np.any(0.058084 < np.array([0.708073, 0.969910, 0.212339, 0.183405, 0.524756, 0.291229]))
4 0.601115 0.708073 True <- np.any(0.601115 < np.array([0.969910, 0.212339, 0.183405, 0.524756, 0.291229]))
5 0.020584 0.969910 True <- np.any(0.020584 < np.array([0.212339, 0.183405, 0.524756, 0.291229]))
6 0.832443 0.212339 False <- np.any(0.832443 < np.array([0.183405, 0.524756, 0.291229]))
7 0.181825 0.183405 True <- np.any(0.181825 < np.array([0.524756, 0.291229]))
8 0.304242 0.524756 False <- np.any(0.304242 < np.array([0.291229]))
9 0.431945 0.291229 UNDEFINED <- Ignore this
使用 for 循环应该可以实现上述操作,但是 pandas/numpy 的方法是什么?
我正在尝试一种方法,将 lambda 函数应用于a,但我找不到一种方法来获取相应a 值的索引来进行np.any 比较,如上所示。 (我后来发现apply 只是 for 循环的语法糖)
df['c'] = df['a'].apply(lambda x: np.any(x < df['b'].values[<i>:])) # Where <i> is the respective index value of x; which I didn't know how to find
【问题讨论】:
标签: python pandas numpy vectorization