I'm trying to convert this code :
df = pd.DataFrame({'values': [np.nan,-1.2,3,6,7,2]})
def f_evol (df) :
list1 = []
for i in range (len(df)) :
if df['values'].isnull()[i] :
list1.append(0)
else :
if df['values'][i] > 0 :
k = list1[i-1] 1
list1.append(k)
if df['values'][i] < 0:
k = list1[i-1] - 1
list1.append(k)
return list1
df['count'] = f_evol(df)
on this one :
def v_evol (df) :
k = [1]
a = np.where(df['values'].isnull().shift(0).values,
k,
np.where(df['values'].shift(0).values > 0,
k.append(k[-1] 1),
np.where(df['values'].shift(0).values < 0,
k.append(k[-1] - 1),
np.nan
)
)
)
return a
i failed to append the last value of k like in the first code ... i tried, to replace ‘k[0] 1’ by ‘np.concatenate()’, by ‘np.append()’ without succes... b
CodePudding user response:
With numpy
This will give you want you want without any explicit iteration (generally, you want to avoid for
loops when working with arrays and dataframes):
np.sign(df["values"].fillna(0)).cumsum()
Demo:
In [3]: df["count"] = np.sign(df["values"].fillna(0)).cumsum()
In [4]: df
Out[4]:
values count
0 NaN 0.0
1 -1.2 -1.0
2 3.0 0.0
3 6.0 1.0
4 7.0 2.0
5 2.0 3.0
You can certainly cast the "count"
column to an integer dtype
if you want.
Without numpy
This will produce the same result without the numpy dependency, if that matters:
(df["values"] // df["values"].abs()).fillna(0).cumsum()