将变量作为pandas iloc函数参数插入

问题描述 投票:0回答:1

我想使用变量插入iloc参数

attribute_name = ['CP_ST', 'CP_ES', 'CP_AD', 'CP_ININ', 'CP_IMIN', 'CP_CL', 'CP_CF', 'CP_SM', 'CP_TL', 'CP_COV']
indices = 1
list_of_attr = ""
for str in attribute_name :
    if(indices != len(attribute_name)):
        list_of_attr += "'" + str + "'"
        list_of_attr += ","
        indices +=1
    else:
        list_of_attr += "'" + str + "'"
        indices +=1

hope_df = hope_df.loc[ : , [list_of_attr] ]

我想将“ list_of_attr”传递给iloc函数

python pandas
1个回答
0
投票

您应该直接在列表中传递属性名称:


attribute_name = ['CP_ST', 'CP_ES', 'CP_AD', 'CP_ININ', 'CP_IMIN', 'CP_CL', 'CP_CF', 'CP_SM', 'CP_TL', 'CP_COV']

hope_df = hope_df.loc[ : , attribute_name ]

© www.soinside.com 2019 - 2024. All rights reserved.