pandas使用apply多列生成一列数据的实例

如下所示:

import pandas as pd
def my_min(a, b):
  return min(abs(a),abs(b))
s = pd.Series([10.0247,10.0470, 10.0647,10.0761,15.0800,10.0761,10.0647,10.0470,10.0247,10.0,9.9753,9.9530,9.9353,9.9239,18.92,9.9239,9.9353,9.9530,9.9753,10.0])
df = pd.DataFrame(s)
df.columns=['value']
df['val_1'] = df['value'].diff()
df['val_2'] = df['val_1'].shift(-1)
df['val'] = df.apply(lambda row: my_min(row['val_1'], row['val_2']), axis=1)

相关推荐