I have a dataframe and I want to check whether the elements in the column \"GDP\"
increase or decrease in comparison to their previous value to create column
out = np.where(df.GDP.diff() > 0, 'increase', 'decline')
out[0] = '------'
numpy
concept #1set up category array and slice it
cats = np.array(['decline', '------', 'increase'])
df.assign(
change=cats[np.sign(
np.append(0, np.diff(df.GDP.values, 1))
).astype(np.uint8) + 1])
numpy
concept #2nested np.where
diffs = df.GDP.diff()
df.assign(
change=np.where(
diffs > 0, 'increase', np.where(
diffs < 0, 'decline', '------')))