字符串中的变量名

这可能吗?


i = 1

for var in ['var1', 'var2', 'var3', 'var4']:

    eval(var) = i

    i + 1

不知何故,我想为每个循环创建一个新变量(具有新名称)。


[编辑:] 很多人说,它没有用,所以下面是我想将它用于的实际代码:


import pandas as pd


# Convert categorical variables to dummies

PPE_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)


潇潇雨雨
浏览 136回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python