-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pandasai ordering custom order not correct #1222
Comments
To sort a custom priority column in a DataFrame with values "P0 - Critical", "P1 - High", "P2 - Medium", and "P3 - Low" from highest to lowest priority using pandasai version 2.0.42, you can use the following code: import pandas as pd
# Sample DataFrame
data = {
'Task': ['Task1', 'Task2', 'Task3', 'Task4'],
'Priority': ['P2 - Medium', 'P0 - Critical', 'P3 - Low', 'P1 - High']
}
df = pd.DataFrame(data)
# Define the custom priority order
priority_order = ['P0 - Critical', 'P1 - High', 'P2 - Medium', 'P3 - Low']
# Convert the Priority column to a categorical type with the specified order
df['Priority'] = pd.Categorical(df['Priority'], categories=priority_order, ordered=True)
# Sort the DataFrame by the Priority column
df_sorted = df.sort_values('Priority')
print(df_sorted) This code will sort the DataFrame |
@dosu I am trying to use pandasai Agent, not interacting with df directly.
|
To ensure that the
Here's an example: from pandasai.agent.base import BaseAgent
from pandasai.pipelines.chat.generate_chat_pipeline import GenerateChatPipeline
import pandas as pd
class CustomSortPipeline(GenerateChatPipeline):
def __init__(self, context, logger, **callbacks):
super().__init__(context, logger, **callbacks)
def custom_sort(self, df: pd.DataFrame, priority_order: list):
# Implement your custom sorting logic here
df['priority'] = pd.Categorical(df['Priority'], categories=priority_order, ordered=True)
df = df.sort_values('priority')
df = df.drop(columns=['priority'])
return df
# Sample DataFrame
data = {
'Task': ['Task1', 'Task2', 'Task3', 'Task4'],
'Priority': ['P2 - Medium', 'P0 - Critical', 'P3 - Low', 'P1 - High']
}
df = pd.DataFrame(data)
# Define the custom priority order
priority_order = ['P0 - Critical', 'P1 - High', 'P2 - Medium', 'P3 - Low']
# Create an instance of the custom pipeline
custom_pipeline = CustomSortPipeline(context=None, logger=None)
# Create an instance of the agent and assign the custom pipeline
agent = BaseAgent(dfs=df)
agent.pipeline = custom_pipeline
# Use the custom sort method
sorted_df = custom_pipeline.custom_sort(df, priority_order)
print(sorted_df) In this example, the |
System Info
pandasai version 2.0.42
馃悰 Describe the bug
The table I used have priority column, from high to low: "P0 - Critical, P1 - High, P2 - Medium, P3 - Low". I asked the agent to order from highest to lowest, but it always generated codes with
.sort_values('priority', ascending=False)
which actually should be.sort_values('priority')
. Sometimes it even generated code withnlargest
so will have errorTypeError: Column 'priority' has dtype object, cannot use method 'nlargest' with this dtype
. I have tried putting this order in input or putting in field_descriptions, but both don't work. How to solve this issue?The text was updated successfully, but these errors were encountered: