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] # 可以
将列名保存为变量 不可以用后缀引用 必须用中括号
阅读 21
2023-08-16
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)