首页 > 解决方案 > 使用 pandas 和“loc”在 DataFrame(使用 read_excel)中查找值,但不区分大小写

问题描述

我是 Pandas (python) 的初学者。我正在使用它导入 xlsx 文件。

read_excel 函数很好,但是当我搜索字符串值时,它只返回确切的模式(区分大小写)。

我在文档中找不到如何忽略这种情况。我的代码:

def read_food(self):
    if len(self.food_filename) > 0 and os.path.exists(self.food_filename):
        self.data_frame = pd.read_excel(self.food_filename, skiprows=3, sheet_name=self.sheetname_ingredients, usecols='B,D,G,J', names=['name', 'food_ingredient_for_net_weight', 'foodges_ingredient_for_ingested_weight', 'food_transport_ingredient_weight'], index_col=None, header=None, dtype={'name':str, 'food_ingredient_for_ingested_weight':float, 'food_ingredient_for_ingested_weight':float, 'food_transport_ingredient_weight':float})
    else:
        raise Exception('[read_food] : food filename/file not exists')

但是,当我用那个搜索一个值时:

return self.data_frame.loc[self.data_frame['name'] == ingredient]

只有具有确切大小写的值才会返回...... :(

请问你有什么想法吗?谢谢

F。

标签: pythonpandascase-sensitive

解决方案


推荐阅读