从用户输入调用特定的 Pandas Dataframe 以在函数中使用?
这可能已经得到解答,但我无法完全找到合适的单词组来搜索以找到我遇到的问题的答案。
情况: 我有几个可以插入到函数中的数据框。该函数要求我命名数据框,以便它能够成形。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议将您的每个数据范围分配给字典,然后通过名称从字典中检索数据框,以将其传递到
heatmap
函数。例如:
在第一行中替换
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:
Replace
pd.DataFrame()
in the first line with the actual data frames that you want to select from.