docs/v3/chat-and-output.mdx
The .chat() method is PandasAI's core feature that enables natural language interaction with your data. It allows you to:
import pandasai as pai
df_customers = pai.read_csv("customers.csv")
response = df_customers.chat("Which are our top 5 customers?")
import pandasai as pai
df_customers = pai.read_csv("customers.csv")
df_orders = pai.read_csv("orders.csv")
df_products = pai.read_csv("products.csv")
response = pai.chat('Who are our top 5 customers and what products do they buy most frequently?', df_customers, df_orders, df_products)
PandasAI supports multiple output formats for responses, each designed to handle different types of data and analysis results effectively. This document outlines the available output formats and their use cases.
Used when the result is a pandas DataFrame. This format preserves the tabular structure of your data and allows for further data manipulation.
Handles visualization outputs, supporting various types of charts and plots generated during data analysis.
Returns textual responses, explanations, and insights about your data in a readable format.
Specialized format for numerical outputs, typically used for calculations, statistics, and metrics.
Provides structured error information when something goes wrong during the analysis process.
The response format is automatically determined based on the type of analysis performed and the nature of the output. You don't need to explicitly specify the format - PandasAI will choose the most appropriate one for your results.
Example:
import pandasai as pai
df = pai.read_csv("users.csv")
response = df.chat("Who is the user with the highest age?") # Returns a String response
response = df.chat("How many users in total?") # Returns a Number response
response = df.chat("Show me the data") # Returns a DataFrame response
response = df.chat("Plot the distribution") # Returns a Chart response
Each response type is designed to handle specific use cases:
The response system is extensible and type-safe, ensuring that outputs are properly formatted and handled according to their specific requirements.
The response object provides several useful methods and properties to interact with the results:
By default, when you print a response object, it automatically returns its .value property:
response = df.chat("What is the average age?")
print(response) # Automatically calls response.value
# Output: The average age is 34.5 years
# For charts, printing will display the visualization
chart_response = df.chat("Plot age distribution")
print(chart_response) # Displays the chart
You can inspect the code that was generated to produce the result:
response = df.chat("Calculate the correlation between age and salary")
print(response.last_code_executed)
# Output: df['age'].corr(df['salary'])
For chart responses, you can save the visualization to a file:
chart_response = df.chat("Create a scatter plot of age vs salary")
chart_response.save("scatter_plot.png") # Saves the chart as PNG