首页 > 解决方案 > 在 for 循环中打印变量的名称而不是变量本身

问题描述

我有几个数据框:df_1、df_2、df_3、df_4 等。

我正在尝试遍历它们,执行操作并输出数据框的名称而不是数据框本身。

for k in [df_1, df_2, df_3, df_4]:
    k.RandomField.value_counts() #or whatever operations
    print(k) #this is where I want it to show "df_1", "df_2", etc instead of printing the variable which, in this case, would be the data frame

我知道我可以创建一个字典,但这需要手动生成一个。感谢你的帮助。

[我正在使用 Jupyter 笔记本并运行 Python 3.8。]

标签: pandasdataframefor-loopnamespaces

解决方案


推荐阅读