例如,我有两个系列
X = pd.Series([0,3,4,0,-1,0])
Y = pd.Series([4,2,5,1,3,5])
我想生成一个新的Z系列。
import pandas as pd
Z = []
for index in range(0,len(X)):
if(X.iloc[index]!=0):
temp = #(Do some arithmetic)
Z.append(temp)
elif(X.iloc[index]==0):
temp = #(Do some arithmetic)
Z.append(temp)
由于效率,是否可以不需要使用For-Loop?
也许喜欢:
Z = Y*Y [X != 0]
Z = Y*Y*Y [X == 0]
#I know this is wrong,but I dont know how to correct it
我不熟悉大熊猫,请告诉我,tks!
答案 0 :(得分:2)
IIUC无论您是添加还是减去零值都无关紧要:
In [245]: (Y-X)/X.abs()
Out[245]:
0 3.000000
1 -0.333333
2 0.250000
3 inf
4 4.000000
5 NaN
dtype: float64