为了账号安全,请及时绑定邮箱和手机立即绑定

字符串中的变量名

字符串中的变量名

潇潇雨雨 2021-06-14 17:01:41
这可能吗?i = 1for var in ['var1', 'var2', 'var3', 'var4']:    eval(var) = i    i + 1不知何故,我想为每个循环创建一个新变量(具有新名称)。[编辑:] 很多人说,它没有用,所以下面是我想将它用于的实际代码:import pandas as pd# Convert categorical variables to dummiesPPE_WAW_DISTRICT = pd.get_dummies(X['PPE_WAW_DISTRICT'], prefix='PPE_WAW_DISTRICT')PPE_TARIFF_TYPE = pd.get_dummies(X['PPE_TARIFF_TYPE'], prefix='PPE_TARIFF_TYPE')PPE_OSD = pd.get_dummies(X['PPE_OSD'], prefix='PPE_OSD')VariantProdEE = pd.get_dummies(X['VariantProdEE'], prefix='VariantProdEE')CUS_OWNER = pd.get_dummies(X['CUS_OWNER'], prefix='CUS_OWNER')CUS_LEGAL_FORM = pd.get_dummies(X['CUS_LEGAL_FORM'], prefix='CUS_LEGAL_FORM')CUS_CITY = pd.get_dummies(X['CUS_CITY'], prefix='CUS_CITY')CUS_DISTRICT = pd.get_dummies(X['CUS_DISTRICT'], prefix='CUS_DISTRICT')X.drop(['PPE_WAW_DISTRICT', 'PPE_TARIFF_TYPE', 'PPE_OSD', 'CUS_OWNER',                'CUS_LEGAL_FORM', 'CUS_CITY', 'CUS_DISTRICT'], axis=1, inplace=True)X = pd.concat([PPE_WAW_DISTRICT, PPE_TARIFF_TYPE, PPE_OSD, VariantProdEE,              CUS_OWNER, CUS_LEGAL_FORM, CUS_CITY, CUS_DISTRICT], axis=1)基本上我想从每个选定的 oclumns 创建虚拟列,然后使用前缀将其添加到主数据框 (X)
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信