0

晚安,伙计们。首先,我想说我是 Python 的初学者。另外,我的英语也不是很好。我正在开发一个自动化 Excel 数据的项目。有一张名为 TACO 的表格,其中包含有关食物的所有营养信息。我的任务是获取特定的食物编号(所有食物都有特定的编号,例如:炸鸡 = 145),这将由第一次输入的人输入,取食物克数,由第二次输入的人输入输入,并根据该信息创建一个数据框。我想我已经解决了第一部分。

问题是:我必须获取收集到的食物编号,然后我必须在 TACO 表中搜索这些食物编号。在此表中找到数字后,我必须从 TACO 表中剪切整行并将其粘贴到新的数据框中,将值乘以克 (input2) 再除以 100。

alimentos_taco = {}
alimentos_taco["Number"] = []
alimentos_taco["Grams"] = []

while True:
    try:
        number = input("Type the food number (or type ENTER if you don't have more foods): ")
        if number == "":
            print("No more foods")
            break
        
        alimentos_taco["Number"].append(int(number))
    except ValueError:
        print("Type a valid number:")
        continue
    try:  
        quantidade = int(input("Type the food quantity (in grams)"))
        alimentos_taco["Grams"].append(quantidade)
    except ValueError:
        print("Type only numbers")
        continue
       

df = pd.DataFrame(alimentos_taco)

# tt_numbers shows up the food numbers I need to search in the TACO table
tt_numbers = list(df["Number"].astype(int))
tt_numbers

# now I have to read the TACO table
tt = pd.read_excel("C:\\Users\\guilh\\modified_tt.xlsx")
pd.set_option("display.max_columns", 30)
pd.set_option("display.max_rows", 695)

# i'm struggling in this part. I tried something to find these numbers in the modified_tt file and after found, append the row to the df dataframe.
for number in tt_numbers:
    if number in tt["Número do alimento"].astype(str):
        df.append(tt.iloc[number])
# here, this appended row should be multiplied by the df["Grams"] values
    else:
        pass
        

我知道你们无权访问 modified_tt 文件,但基本上我必须在 tt["Número do alimento"] 列中搜索数字。

4

0 回答 0