如何使用for循环在python DataFram中乘以各个值

2024-06-16 10:57:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何使用for循环,当我想在python中更改数据帧(panda)中的单个值时

例如,我有以下数据帧,包含NFSE和DAX指数的价格。在

   Date       NFSE   DAX
2018/02/01  32.085  43.042
2018/02/02  31.036  43.023
2018/02/03  30.013  42.016

我想把所有给定的价格转换成对数回报率的百分比:rt=100*np.日志(Pt/Pt−1)。在

我如何使用for循环,它将所有的价格相互作用,创建一个新的数据帧,并以百分比的日志返回?在

提前谢谢你!在

干杯

朱利安


Tags: 数据ptfordatenp对数价格指数
1条回答
网友
1楼 · 发布于 2024-06-16 10:57:16

这是否提供了所需的输出?在

# pick the columns you wish to calculate
# a single column must be in a list
col_list = ['NFSE', 'DAX']
# create a new dataframe with only columns from col_list
df_log = df[col_list].copy()

# you can use a loop to run through your col_list columns
for col in df_log.columns:
    df_log[col] = 100 * np.log(df_log[col] / df_log[col].shift())

print(df_log)

                 NFSE         DAX
2018-02-01        NaN         NaN
2018-02-02  -3.324081   -0.044153
2018-02-03  -3.351720   -2.368436

设置df

^{pr2}$

相关问题 更多 >