根据您提供的代码,问题出在[2830000](https://github.com/run-llama/llama_index/commit/28300004c9e1fe89e3e562eb7a634b65b8cc75ad)
和[1350000](https://github.com/run-llama/llama_index/commit/1350000b9ad754be5fd9749e03e0f828e0a4d705)
这两个变量上。您需要将它们替换为实际的数值。以下是修改后的代码:
import pandas as pd
from llama_index.experimental.query_engine import PandasQueryEngine
from llama_index.llms.openai import OpenAI
import os
df = pd.DataFrame(
{
"country": [
"United States",
"United Kingdom",
"France",
"Germany",
"Italy",
"Spain",
"Canada",
"Australia",
"Japan",
"China",
],
"gdp": [
21400000,
2940000,
3870000, # 修改这里
3870000, # 修改这里
2160000,
3870000, # 修改这里
3870000, # 修改这里
1320000,
516000,
14000000,
],
"happiness": [7.3, 7.2, 6.5, 7.0, 6.0, 6.3, 7.3, 7.3, 5.9, 5.0],
}
)
llm = OpenAI()
query_engine = PandasQueryEngine(df=df, verbose=True)
response = query_engine.query(
"Visualize barchat of number of happiness of country .",
)
请将上述代码中的[2830000](https://github.com/run-llama/llama_index/commit/28300004c9e1fe89e3e562eb7a634b65b8cc75ad)
和[1350000](https://github.com/run-llama/llama_index/commit/1350000b9ad754be5fd9749e03e0f828e0a4d705)
替换为实际的数值,然后再次运行。
7条答案
按热度按时间wmvff8tz1#
要解决在使用
pandas_query_engine
生成条形图时出现的"name 'p' is not defined"错误,需要确保绘图库(如matplotlib.pyplot
,因为plt
)被正确导入和使用。提供的代码中没有包含任何绘图逻辑,所以你需要添加必要的导入和绘图代码。以下是如何修改代码以包含绘图逻辑的示例:
此代码包含了对
matplotlib.pyplot
的必要导入,并添加了生成国家幸福感条形图的逻辑。如果需要,请确保用实际使用的模型替换llm
。详细信息
关于Dosu
此响应旨在帮助您节省时间并提高效率。它不是精确解决方案,而是您自己研究的起点。
帮助我变得更有用!如果这有帮助,请留下一个👍,如果不相关,请留下一个👎。
kadbb4592#
如何保存生成的统计图表@dosu.
uemypmqf3#
要保存生成的统计图,可以使用
matplotlib
库中的plt.savefig
函数。以下是修改后的代码示例,展示如何保存生成的统计图:import pandas as pd
import matplotlib.pyplot as plt
from llama_index.experimental.query_engine import PandasQueryEngine
from llama_index.llms.openai import OpenAI
df = pd.DataFrame(
{
"country": [
"United States",
"United Kingdom",
"France",
"Germany",
"Italy",
"Spain",
"Canada",
"Australia",
"Japan",
"China",
],
"gdp": [
21400000,
2940000,
2830000,
3870000,
2160000,
1350000,
1780000,
1320000,
516000,
14000000,
],
"happiness": [7.3, 7.2, 6.5, 7.0, 6.0, 6.3, 7.3, 7.3, 5.9, 5.0],
}
)
llm = OpenAI()
query_engine = PandasQueryEngine(df=df, verbose=True)
response = query_engine.query(
"Visualize bar chart of number of happiness of country.",
)
生成并保存柱状图
df.plot(kind='bar', x='country', y='happiness')
plt.xlabel('Country')
plt.ylabel('Happiness')
plt.title('Happiness by Country')
plt.savefig('happiness_by_country.png') # 保存图表到文件
plt.show()
p4tfgftt4#
我需要在返回响应后直接保存图像@dosu
olmpazwi5#
在使用
pandas_query_engine
时,要在响应返回后直接保存图像,您可以修改_query
方法以包含保存图像的代码。以下是如何做到这一点的示例:在此代码中,在执行pandas代码生成图表后,会调用
plt.savefig('chart.png')
函数,在响应返回后直接保存图表图像。详细信息
关于Dosu
此响应旨在提供帮助并节省您的时间。它不是精确解决方案,而是您自己研究的起点。
帮助我变得更有用!如果这有帮助,请留下一个👍,如果不相关,请留下一个👎。
yqhsw0fo6#
我尝试了你的方法,但是没有成功。请继续报告错误@dosu
回溯(最近的调用):
文件 "E:\anaconda\Lib\site-packages\llama_index\experimental\query_engine\pandas\output_parser.py",第54行,在default_output_processor中
output_str = str(safe_eval(module_end_str, {"np": np}, local_vars))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
文件 "E:\anaconda\Lib\site-packages\llama_indexexperimental\exec_utils.py",第159行,在safe_eval中
return eval(__source, _get_restricted_globals(__globals), __locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^{^^^{^{}^^{^{}^{}}}
文件 "",第1行,在
NameError:名称'p'未定义
qybjjes17#
错误 "name 'p' is not defined" 表明代码中存在缺失的导入或拼写错误。具体来说,代码似乎试图使用一个名为
p
的变量或函数,但尚未定义。要解决此问题,请确保正确导入和使用所有必要的库。以下是包含必要导入并保存生成图表的更新后的代码:
如果您需要在
_query
方法返回响应后立即保存图像,请确保正确导入和使用绘图库。以下是如何修改_query
方法的示例:请确保在脚本开头包含所需的导入(
import matplotlib.pyplot as plt
和import pandas as pd
)。这应解决“name 'p' is not defined”错误,并允许您保存生成的图表图像 1(https://github.com/run-llama/llama_index/blob/main/llama-index-experimental/llama_index/experimental/query_engine/pandas/output_parser.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/query_engine/pandas/output_parser.py)。