从字符串中提取定量信息

时间:2018-08-12 21:23:04

标签: python regex pandas feature-extraction text-extraction

我正在分析开放食品事实数据集。 数据集非常混乱,并且有一个名为“ quantity”的列,其中的条目如下:

'100 g',
'142克(5盎司)',
'12盎司',
'200克',
'340克(12盎司)',
'10盎司(296毫升)',
750毫升,
'1 l',
250毫升 '8 OZ',
750克(10.5盎司),
'1加仑(3.78 L)',
'27盎司(1磅11盎司)765克',
'75 cl',

如您所见,值和度量单位到处都是!有时,数量是通过两种不同的度量给出的... 我的目标是在熊猫数据框中创建一个新列“ quantity_in_g”,在该列中,我从字符串中提取信息,并基于“ quantity”列中的克数创建一个整数Value。 因此,如果“数量”列的值为“ 200 g”,我希望整数为200,如果它说“ 1 kg”,则希望整数为1000。我还想将其他度量单位转换为克。对于“ 2盎司”,我想要整数56,对于1 L,我想要1000。
有人可以帮我转换此列吗? 我真的很感激!
预先感谢

1 个答案:

答案 0 :(得分:0)

raw_data_lst = ['100 g ','5 oz (142 g)','12 oz','200 g ','12 oz (340 g)','10 f oz (296ml)','750 ml','1 l','250 ml', '8 OZ',] 
# 10 f oz (296ml)  don't know what f is
# if more there is more data like this then gram_conv_dict.keys() loop over this instead of directly ... doing what i have done below

in_grams_colm = []
gram_conv_dict ={
    'g':1,
    'oz': 28.3495,
    'kg':1000,
    'l': 1000 # assuming 1 litre of water --> grams
    }
# ml --> g is tricky as density varies

def convert2num(string_num):
    try:
        return int(string_num)
    except ValueError:
        return float(string_num)

def get_in_grams(unit):
    try:
        return gram_conv_dict[unit.lower()]
    except:
        print('don\'t know how much grams is present in 1',unit+'.')

    return 1


for data in raw_data_lst:
    i = 0
    quantity_str =''
    quantity_num = 0
    while i < len(data):
        if  47 < ord(data[i]) < 58 or data[i] == '.':
            quantity_str+= data[i]
        else:
            # data[i] = '' most abbrv has at most length = 2 therefore data[i+1:i+3] or u can just send the whole data[i+1:]
            # gram_conv_dict[data[i+1:i+3].strip()] directly check if key exist
            break

        i+=1

    quantity_num = convert2num(quantity_str)*get_in_grams(data[i+1:i+3].strip()) # assuming each data has this format numberspace-- len 2 abbrv
    in_grams_colm.append(quantity_num) # if u want only integer int(quantity_num)

#print(in_grams_colm)

def nice_print():
    for _ in in_grams_colm:
        print('{:.2f}'.format(_))

nice_print()
'''
output

don't know how much grams is present in 1 f.
don't know how much grams is present in 1 ml.
don't know how much grams is present in 1 ml.
100.00
141.75
340.19
200.00
340.19
10.00
750.00
1000.00
250.00
226.80'''
相关问题