答案 0 :(得分:2)
out = np.where(df.GDP.diff() > 0, 'increase', 'decline')
out[0] = '------'
答案 1 :(得分:2)
numpy
概念#1 设置类别数组并对其进行切片
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
概念#2 嵌套np.where
diffs = df.GDP.diff()
df.assign(
change=np.where(
diffs > 0, 'increase', np.where(
diffs < 0, 'decline', '------')))