Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除数据帧列中的多个子字符串_Python_Regex_Pandas - Fatal编程技术网

Python 删除数据帧列中的多个子字符串

Python 删除数据帧列中的多个子字符串,python,regex,pandas,Python,Regex,Pandas,我在熊猫数据框中有一列成分。我需要除去除配料名称以外的所有成分(例如:1/3杯腰果>腰果) 输入 预期产量 我尝试过使用字典,将常用词映射到空字符串,如下所示: remove_list ={'\d+': '', 'ounces': '', 'ounce': '', 'tablespoons': '', 'tablespoon': '', 'teaspoons': '', 'teaspoon': '', 'cup': '', 'cups': ''} column = df['ingredient'

我在熊猫数据框中有一列成分。我需要除去除配料名称以外的所有成分(例如:1/3杯腰果>腰果)

输入

预期产量

我尝试过使用字典,将常用词映射到空字符串,如下所示:

remove_list ={'\d+': '', 'ounces': '', 'ounce': '', 'tablespoons': '', 'tablespoon': '', 'teaspoons': '', 'teaspoon': '', 'cup': '', 'cups': ''}
column = df['ingredient']
column.apply(lambda column: [remove_list[y] if y in remove_list else y for y in column])
这根本没有改变数据

我也尝试过使用正则表达式:

df['ingredients'] = re.sub(r'|'.join(map(re.escape, remove_list)), '', df['ingredients'])
但这只会给出一个错误,即“TypeError:预期的字符串或缓冲区”


我对Python非常陌生,因此我认为使用正则表达式是可能的,我只是不知道如何做到这一点。

熊猫数据帧中内置了一组字符串函数

像这样的方法应该会奏效:

df['ingredient'] = df['ingredient'].str.replace('\d+', '', regex=True)
我不知道你是否可以使用dict,你可能需要反复阅读你的字典才能得到你想要的所有替换

for ptn, rpl in remove_list.items():
    df['ingredient'] = df['ingredient'].str.replace(ptn, rpl, regex=True)

因为您想用相同的字符替换所有内容,所以只需将它们放入列表中即可

l = ['\d+', '[^\x00-\x80]+', 'ounces', 'ounce', 'tablespoons', 
     'tablespoon', 'teaspoons', 'teaspoon', 'cup', 'cups']
然后使用一个
替换
,连接所有内容

df.ingredient.str.replace('|'.join(l), '', regex=True).str.strip()
# Safer to only replace stand-alone words. strip not needed
#df.ingredient.str.replace('|'.join([x + '\s' for x in l]), '', regex=True)
输出:
我将
'[^\x00-\x80]+'
添加到列表中以删除这些小数字符,并且
.str.strip
在替换后删除任何多余的或前导的空格。

您可以使用循环和
.split()
方法:

i = 0
for row in df['ingredient']:
    item = row.split(sep=' ', maxsplit=1)
    df['ingredient'].loc[i] = item[1]
    i += 1
输出将是:

    recipe_name                                ingredient
0   Truvani Chocolate Turmeric Caramel Cups    cup cashews
1   Truvani Chocolate Turmeric Caramel Cups    dates
2   Truvani Chocolate Turmeric Caramel Cups    tablespoon almond butter
3   Truvani Chocolate Turmeric Caramel Cups    tablespoons coconut milk
4   Truvani Chocolate Turmeric Caramel Cups    teaspoon vanilla extract

如果你想保留度量值,你可以创建一个重复的列,在一列中保留值,在另一列中保留成分。

我试图做到这一点,而不是对每个单词使用单独的替换语句,但这可能是你使用dict的唯一方法,你只需要用一个重复模式额外的代码行(如上所述)。@Conor请小心,因为这会意外地将
5个纸杯蛋糕
替换为
蛋糕
。你可以通过替换
'\cup\s'
来解决这个问题,这样它只会在后面跟一个空格时替换单词,就像
'cup'
中那样,而不是
'cupc'
啊,谢谢你,我刚才检查过了,因为我遇到了这个问题。
0            cashews
1              dates
2      almond butter
3       coconut milk
4    vanilla extract
Name: ingredient, dtype: object
i = 0
for row in df['ingredient']:
    item = row.split(sep=' ', maxsplit=1)
    df['ingredient'].loc[i] = item[1]
    i += 1
    recipe_name                                ingredient
0   Truvani Chocolate Turmeric Caramel Cups    cup cashews
1   Truvani Chocolate Turmeric Caramel Cups    dates
2   Truvani Chocolate Turmeric Caramel Cups    tablespoon almond butter
3   Truvani Chocolate Turmeric Caramel Cups    tablespoons coconut milk
4   Truvani Chocolate Turmeric Caramel Cups    teaspoon vanilla extract