Comments (3)
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 df
by the Priority
column according to the custom order defined in priority_order
. The sorted DataFrame df_sorted
will have the tasks ordered from highest to lowest priority [1].
To continue talking to Dosu, mention @dosu.
from pandas-ai.
To ensure that the pandasai
Agent respects your custom priority order when processing queries, you can extend the agent with a custom pipeline that includes your sorting logic. Here's how you can do it:
- Create a Custom Pipeline: Define a custom pipeline that includes your sorting logic.
- Integrate the Custom Pipeline with the Agent: Use this custom pipeline with the
pandasai
Agent.
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 CustomSortPipeline
class is a subclass of GenerateChatPipeline
where the custom_sort
method implements the custom sorting logic. The BaseAgent
is then configured to use this custom pipeline, ensuring that the DataFrame is sorted according to your custom priority order [1][2].
To continue talking to Dosu, mention @dosu.
from pandas-ai.
@dosu I am trying to use pandasai Agent, not interacting with df directly.
response = agent.chat("Return 5 tasks which have higher priority")
from pandas-ai.
Related Issues (20)
- Issue with PandasAI: KeyError: '__import__' during Code Execution HOT 2
- Questions about the train function HOT 1
- pandas-ai fails with polars.scan_parquet input HOT 2
- issue with running this code - from pandasai.llm.local_llm import LocalLLM HOT 3
- issue importing models from hugging face HOT 8
- Return plots as json strings
- docs: add AWS Bedrock tutorial to the example
- Issue in importing pandsai HOT 3
- unable to run google models - from pandasai.llm import GooglePalm HOT 1
- output issue HOT 2
- Issue on docs HOT 1
- Issue in output table conflict with index and column data HOT 2
- how to train using GooglePalm - MissingVectorStoreError: No vector store provided. Please provide a vector store to train the agent. HOT 6
- how to deal with multiple dataframes HOT 2
- Ollama models are not working with Loca;LLM HOT 1
- No results with local llama models HOT 1
- Unable to analyze the DataFrame when it contains data in list format. HOT 1
- Using local model with PandasAI results in error HOT 1
- Feature generation HOT 1
- use openai error(openai.BadRequestError: Error code: 400) HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas-ai.