从用户输入调用特定的 Pandas Dataframe 以在函数中使用?

发布于 2025-01-17 14:48:37 字数 644 浏览 3 评论 0原文

这可能已经得到解答,但我无法完全找到合适的单词组来搜索以找到我遇到的问题的答案。

情况: 我有几个可以插入到函数中的数据框。该函数要求我命名数据框,以便它能够成形。

def heatmap(table, cmap=cm.inferno, vmin=None, vmax=None, inner_r=0.25, pie_args={}:
     n, m = table.shape

我希望用户能够指定用作表格的数据框,如下所示:

table_name= input('specify Table to Graph: ')
heatmap(table_name)

期望:如果用户输入是 TableXYZ,则变量 table_name 将引用 TableXYZ,以便该函数能够找到TableXYZ 的形状以在函数的其他部分使用该信息。

实际发生的情况:当我尝试运行代码时,我收到“AttribureError:'str'没有属性'shape'”。我看到 table_name 输入是一个字符串对象,但我试图引用数据框本身,而不是名称。

我觉得我缺少将用户的输入转变为函数实际上可以呈现的形式的步骤。

This might have been answered, but I can't quite find the right group of words to search for to find the answer to the problem I'm having.

Situation:
I have a several data frames that could be plugged into a function. The function requires that I name the data frame so that it can take the shape.

def heatmap(table, cmap=cm.inferno, vmin=None, vmax=None, inner_r=0.25, pie_args={}:
     n, m = table.shape

I want the user to be able to specify the data frame to use as the table like this:

table_name= input('specify Table to Graph: ')
heatmap(table_name)

Expectation: If the user input was TableXYZ then the variable table_name would reference TableXYZ so the function would be able to find the shape of TableXYZ to use that information in other parts of the function.

What actually happens: When I try to run the code I get an "AttribureError: 'str' has not attribute 'shape'." I see that the table_name input is a string object, but I'm trying to reference the data frame itself, not the name.

I feel like I'm missing a step to turn the user's input to something the function can actually take the shape of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2025-01-24 14:48:37

我建议将您的每个数据范围分配给字典,然后通过名称从字典中检索数据框,以将其传递到heatmap函数。

例如:

df_by_name = {"df_a": pd.DataFrame(), "df_b": pd.DataFrame()}
table_name= input('specify Table to Graph: ')
df = df_by_name[table_name]
heatmap(df)

在第一行中替换pd.dataframe(),使用您要选择的实际数据框架。

I'd recommend assigning each of your dataframes to a dictionary, then retrieving the dataframe by name from the dictionary to pass it to the heatmap function.

For example:

df_by_name = {"df_a": pd.DataFrame(), "df_b": pd.DataFrame()}
table_name= input('specify Table to Graph: ')
df = df_by_name[table_name]
heatmap(df)

Replace pd.DataFrame() in the first line with the actual data frames that you want to select from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文