0
点赞
收藏
分享

微信扫一扫

将列名保存为变量 不可以用后缀引用 必须用中括号

陬者 2023-08-16 阅读 20

df_1 = pd.DataFrame(np.arange(9).reshape(3,3))
df_1.columns = ['col' + str(i) for i in df_1.columns]
str_colname = 'col1'
# df_1.str_colname  # AttributeError: 'DataFrame' object has no attribute 'str_colname'
df_1[str_colname]  # 可以

举报

相关推荐

0 条评论