Giter Site home page Giter Site logo

Comments (3)

dosubot avatar dosubot commented on September 27, 2024 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.

dosubot avatar dosubot commented on September 27, 2024 3

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:

  1. Create a Custom Pipeline: Define a custom pipeline that includes your sorting logic.
  2. 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.

ssling0817 avatar ssling0817 commented on September 27, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.