通过 Pandas Dataframe 像 switch 语句一样广播的巧妙方法

Clever way to broadcast like a switch statement through a Pandas Dataframe

我有分段操作,我会用其他语言的开关来做。有没有一种方法可以通过切片或广播使我可以高效地执行以下操作?数据结构是一个多索引 pandas Dataframe.

def spreadStat(data):
data.loc[data.Pval - data.Tval > 0 and data.Tval.value() > 75, 'Tval'] = data['Tval'] - 500
data.loc[data.Pval - data.Tval > 0 and data.Tval.value() < 25, 'Tval'] = data['Tval'] - 400
data.loc[data.Pval - data.Tval > 0 and 25 < data.Tval.value() < 40, 'Tval'] = data['Tval'] - 300
data.loc[data.Pval - data.Tval > 0 and 60 < data.Tval.value() < 75, 'Tval'] = data['Tval'] - 200
data.loc[data.Pval - data.Tval > 0 and 40 < data.Tval.value() < 60, 'Tval'] = data['Tval'] - 100

这似乎是单行代码或某个函数的映射?任何帮助将不胜感激...

cut:

bins = [-np.inf, 25, 40, 60, 75, np.inf]
vals = [400, 300, 200, 100, 500]

# I didn't test, but `astype` might not be necessary
to_subtract = pd.cut(data.Tval, bins=bins, labels=vals).astype(float)

data.loc[data.Pval > data.Tval, 'Tval'] -= to_subtract