我有以下功能
macd, macdsignal, macdhist = talib.MACD(df.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
我想将这些新值作为现有数据框的一部分,我该怎么做? 这就是我试过的
df['macd'], df['macdsignal'], df['macdhist'] = talib.MACD(df.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
编辑以添加前后
之前
Close
10
10
10
在
Close macd macdsignal macdhist
10 1.1 3.8 5.7
10 3.2 8.7 4.2
10 1.9 9.5 1.2
答案 0 :(得分:1)
假设talib.MACD(df.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
返回列表列表,您可以获得以下两件事:
lofl = talib.MACD(df.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
df[['macd', 'macdsignal', 'macdhist']] = pd.DataFrame.from_records(zip(*lofl))
df.assign
:lofl = talib.MACD(df.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
new_cols = ['macd', 'macdsignal', 'macdhist']
d = {k:v for k,v in zip(new_cols, lofl)}
df = df.assign(**d)
我希望这证明有用。