File size: 728 Bytes
9bdca4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pandas as pd

df = pd.read_excel("./vietnamekcal.xlsx")

def convert_quantity(quantity):
    quantity_parts = quantity.strip().split(' ')
    amount = quantity_parts[0]
    detail = ' '.join(quantity_parts[1:])
    try:
        return float(amount), detail
    except ValueError:
        return amount, detail

new_df = pd.DataFrame()
new_df['food'] = df['THỨC ĂN'].str.lower().str.strip()
new_df['amount'], new_df['detail'] = zip(*df['SỐ LƯỢNG'].apply(convert_quantity))
new_df['calo'] = (df['CALO (kcal)'] / new_df['amount'].apply(lambda x: float(x) if isinstance(x, str) else x)).astype(int)
new_df['amount'] = 1 

new_df.to_excel('calories.xlsx', index=False)  # Save as Excel file without including the index