1

I am currently using two data frames and I wish to compare two columns across them, and then create an output variable. However, I want to store the output in the form of a pandas dataframe, or atleast in a json output, which I can then read back to python. My approach is currently using a python dataframe agent, if there's an alternative, would appreciate it.

My datasets look like this

Table 1
| ID  | Food items |
| --- | -----------|
| 1   | Apple      |
| 2   | Celery     |
| 3   | Chicken    |


Table 2
| ID  | Categories |
| ----| -----------|
| 1   | Vegetable  |
| 2   |Fruit       |
| 3   |Meat        |

I would like a mapping of Apple to Fruit, Celery to Vegetable etc in a table format.

4
  • Can you give an example of the input data and the result you're trying to achieve? Commented Nov 4, 2024 at 5:54
  • Have added it now in the question Commented Nov 4, 2024 at 6:07
  • Well, you need to create rules for such mapping, there's no pandas functionality you can use to linguistically match words. Commented Nov 5, 2024 at 0:28
  • I wanted to use the langchain functionality to be able to linguistically match the words. Commented Nov 8, 2024 at 16:18

2 Answers 2

0

With Pandas:

# Assuming your dataframes are named df1 and df2
result = pd.merge(df1, df2, on='ID')

# If you want to just keep the Food items and Categories columns:
result = result[['Food items', 'Categories']]
Sign up to request clarification or add additional context in comments.

Comments

0

When the output is generated, store it to a variable which will be in the form of a python dictionary of input and output. For example if your output is in the format,

 input:{query}, output :{"Apple":"Fruit"} 

Then, save it to a variable

code_response=input:{query}, output :{"Apple":"Fruit"}

From this dictionary, you can extract output as code df = pd.DataFrame(list(data.items()), columns=['Item','Category']) in your standard python dictionary manipulation.

1 Comment

It could be great if you add an example code for anyone wondering how you applied this anwer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.