我想在df中添加一列.这个新df的值将取决于其他列的值.例如
dc = {'A':[0,9,4,5],'B':[6,0,10,12],'C':[1,3,15,18]}
df = pd.DataFrame(dc)
A B C
0 0 6 1
1 9 0 3
2 4 10 15
3 5 12 18
Run Code Online (Sandbox Code Playgroud)
现在我想添加另一个列D,其值取决于A,B,C的值.所以例如,如果迭代通过df,我会这样做:
for row in df.iterrows():
if(row['A'] != 0 and row[B] !=0):
row['D'] = (float(row['A'])/float(row['B']))*row['C']
elif(row['C'] ==0 and row['A'] != 0 and row[B] ==0):
row['D'] == 250.0
else:
row['D'] == 20.0
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有for循环或使用where()或apply()函数的情况下执行此操作.
谢谢