我有简单的数据帧:
import pandas as pd
frame = pd.DataFrame(np.random.randn(4, 3), columns=list('abc'))
Run Code Online (Sandbox Code Playgroud)
例如:
a b c
0 -0.813530 -1.291862 1.330320
1 -1.066475 0.624504 1.690770
2 1.330330 -0.675750 -1.123389
3 0.400109 -1.224936 -1.704173
Run Code Online (Sandbox Code Playgroud)
如果c为正,我想创建包含"c"值的列"d".其他值来自"b".
我在尝试:
frame['d']=frame.apply(lambda x: frame['c'] if frame['c']>0 else frame['b'],axis=0)
Run Code Online (Sandbox Code Playgroud)
但得到"ValueError :('系列的真值是不明确的.使用a.empty,a.bool(),a.item(),a.any()或a.all().','发生于索引a')
我试图谷歌如何解决这个问题,但没有成功.有什么提示吗?