python动态生成数据库表,Python Pandas动态创建数据框

The code below will generate the desired output in ONE dataframe, however, I would like to dynamically create data frames in a FOR loop then assign the shifted value to that data frame. Example, data frame df_lag_12 would only contain column1_t12 and column2_12. Any ideas would be greatly appreciated. I attempted to dynamically create 12 dataframes using the EXEC statement, google searching seems to state this is poor practice.

import pandas as pd

list1=list(range(0,20))

list2=list(range(19,-1,-1))

d={'column1':list(range(0,20)),

'column2':list(range(19,-1,-1))}

df=pd.DataFrame(d)

df_lags=pd.DataFrame()

for col in df.columns:

for i in range(12,0,-1):

df_lags[col+'_t'+str(i)]=df[col].shift(i)

df_lags[col]=df[col].values

print(df_lags)

for df in (range(12,0,-1)):

exec('model_data_lag_'+str(df)+'=pd.DataFrame()')

Desired output for dymanically created dataframe DF_LAGS_12:

var_list=['column1_t12','column2_t12']

df_lags_12=df_lags[var_list]

print(df_lags_12)

解决方案

I think the best is create dictionary of DataFrames:

d = {}

for i in range(12,0,-1):

d['t' + str(i)] = df.shift(i).add_suffix('_t' + str(i))

If need specify columns first:

d = {}

cols = ['column1','column2']

for i in range(12,0,-1):

d['t' + str(i)] = df[cols].shift(i).add_suffix('_t' + str(i))

dict comprehension solution:

d = {'t' + str(i): df.shift(i).add_suffix('_t' + str(i)) for i in range(12,0,-1)}

print (d['t10'])

column1_t10 column2_t10

0 NaN NaN

1 NaN NaN

2 NaN NaN

3 NaN NaN

4 NaN NaN

5 NaN NaN

6 NaN NaN

7 NaN NaN

8 NaN NaN

9 NaN NaN

10 0.0 19.0

11 1.0 18.0

12 2.0 17.0

13 3.0 16.0

14 4.0 15.0

15 5.0 14.0

16 6.0 13.0

17 7.0 12.0

18 8.0 11.0

19 9.0 10.0

EDIT: Is it possible by globals, but much better is dictionary:

d = {}

cols = ['column1','column2']

for i in range(12,0,-1):

globals()['df' + str(i)] = df[cols].shift(i).add_suffix('_t' + str(i))

print (df10)

column1_t10 column2_t10

0 NaN NaN

1 NaN NaN

2 NaN NaN

3 NaN NaN

4 NaN NaN

5 NaN NaN

6 NaN NaN

7 NaN NaN

8 NaN NaN

9 NaN NaN

10 0.0 19.0

11 1.0 18.0

12 2.0 17.0

13 3.0 16.0

14 4.0 15.0

15 5.0 14.0

16 6.0 13.0

17 7.0 12.0

18 8.0 11.0

19 9.0 10.0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值