【发布时间】:2020-12-06 00:03:13
【问题描述】:
df1
price date cost variance
0 152.45 2020-01-04 4 3
...
price = df1.loc[:, ['price', 'date']].set_index('date').dropna()
price
price
date
2020-01-04 152.45
有没有办法针对价格和其他指标快速执行上述操作?我试过了:
metrics = ['price', 'cost', 'variance']
for x in metrics:
globals().update({x:df1.loc[:, [x, 'date']].set_index('date').dropna()})
NameError: name 'price' is not defined
【问题讨论】:
-
欢迎来到 SO!如果您有一个可用的矢量化版本,为什么要将其更改为循环?可以分享
df1的代表sn-p吗?谢谢。
标签: python python-3.x pandas list dataframe