【发布时间】:2019-01-11 08:59:19
【问题描述】:
我正在尝试通过管道运行多个数据帧以永久更改每个数据帧,但更改不会在 for 循环之外持续存在。有人可以告诉我这样做的正确语法吗?所有编辑都像 edit_1g() 一样分配并返回一个数据框。谢谢。
# create pipeline to preprocess the data:
def pipeline_1(df):
df1=(df.pipe(edit_2a)
.pipe(edit_2b)
.pipe(edit_2d)
.pipe(edit_2e)
.pipe(edit_1f)
.pipe(edit_1j)
.pipe(edit_1g)
.pipe(edit_2h)
)
return df1
# list the data frames we want to run through our pipeline:
dfs = {'df_orders':df_orders, 'df_accts_summary':df_accts_summary, 'df_accts1':df_accts1,
'df_traders_summary':df_traders_summary, 'df_traders1':df_traders1,
'df_tag76_summary':df_tag76_summary, 'df_tag761':df_tag761}
print('data frames altered via pipeline_1: \n')
for key, values in dfs.items():
values = pipeline_1(values) # changes aren't persisting outside of the loop
print(key + ' ' + str(values.shape))
# round the decimals of columns:
def edit_1g(df):
d = {'icpwp10bp':0, 'icpwp2bp':0, 'icslippagebpbp':0, 'participationrate':0, 'adv':1, 'twodprioris':0,
'twodpostis':0, 'orderval':0, 'valuedark':0, 'mktvalflt':0, 'numberoffills': 0, 'size':0,
'lmtadjintvwap':0, 'fivedsprd':0, 'tendvol':0
}
df = df.round(d)
return df
【问题讨论】:
-
您能否列出您的编辑功能之一的示例?他们是否获取并返回数据框?
-
是的。我上面已经举了一个例子
-
查看标记的重复项,
values = pipeline_1(values)只是更新变量values。它不会更新您的字典。