0

My data is below

food ID name ingredients ingredient ID amount unit
1 rice red R1 10 g
1 soup blue B1 20 g
1 soup yellow Y1 30 g

and I want to convert it like this


{
  'data': [
    {
      'name': 'rice',
      'ingredients': [
        {
          'name': 'red',
          'ingredient_id':'R1',
          'amount': 10,
          'unit': 'g',
        }
      ]
    },
    {
      'name': 'soup',
      'ingredients': [
        {
          'name': 'blue',
          'ingredient_id':'B1',
          'amount': 20,
          'unit': 'g',
        },
        {
          'name': 'yellow',
          'ingredient_id':'Y1',
          'amount': 30,
          'unit': 'g',
        }
      ]
    }
  ]
}

How can I do it? Do I need to use the same library as pandas?

1 Answer 1

1

Yes you can modify your data by using custom code function inside python.

For your required format you need to use this code for format your data into json.

import pandas as pd

data = [[1, 'rice', 'red', 'R1', 10, 'g'],
    [1, 'soup', 'blue', 'B1', 20, 'g'],
    [1, 'soup', 'yellow', 'Y1', 30, 'g'],
    [1, 'apple', 'yellow', 'Y1', 30, 'g']]

df = pd.DataFrame(data, columns=['food ID', 'name', 'ingredients', 'ingredient ID', 'amount', 'unit'])

def convert_data_group(group):
    ingredients = [{'name': row['ingredients'], 'ingredient_id': row['ingredient ID'], 'amount': row['amount'], 'unit': row['unit']} for _, row in group.iterrows()]
    return {'name': group.iloc[0]['name'], 'ingredients': ingredients}

unique_names = df['name'].unique().tolist()
result = []
for name in unique_names:
    group = df[df['name'] == name]
    result.append(convert_data_group(group))

final_result = {'datas': result}
print(final_result)

Your final result will be:

{'datas': [{'name': 'rice', 'ingredients': [{'name': 'red', 'ingredient_id': 'R1', 'amount': 10, 'unit': 'g'}]}, {'name': 'soup', 'ingredients': [{'name': 'blue', 'ingredient_id': 'B1', 'amount': 20, 'unit': 'g'}, {'name': 'yellow', 'ingredient_id': 'Y1', 'amount': 30, 'unit': 'g'}]}, {'name': 'apple', 'ingredients': [{'name': 'yellow', 'ingredient_id': 'Y1', 'amount': 30, 'unit': 'g'}]}]}

enter image description here

Sign up to request clarification or add additional context in comments.

13 Comments

sir, how can I do sort data not rice , soup but soup, rice? I have many data, so when I adjust your solution, ingredient data order is twisted.
Sorry I can not understand your question. Explain what you want to from result.
If you look at the order of the text, it is printed in order of rice and soup, but if you assume that apple was added last in the table I have, I executed your code. Then, json data (name) is not output in the order of rice, soup, and apple, but in the order of apple, rice, and soup (abc). Do you understand? I'm sorry for my lack of explanation.
Yes. This code will give you result in asc order like apple, rice and soup.
Thank you sir. It is what I wanted. I'm sorry to bother you.
|

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.