Home > Blockchain >  How to add many specific string from a string to a list in columns?
How to add many specific string from a string to a list in columns?

Time:11-08

import pandas as pd
data = {'Product Description': [': da tổng hợp, khung gỗ sồi canada.', ': gỗ mfc phủ melamine, chân thép sơn tĩnh']}
df = pd.DataFrame(data)
def add_material(row):
    lst = []   
    temp = row['Product Description'] 
    for i in temp:
        if 'da tổng hợp' in temp:
            lst.append('Synthetic Leather')
        elif 'gỗ sồi' in temp:
            lst.append('Oak')
        elif 'gỗ mfc phủ melamine' in temp:
            lst.append('Melamine coated MFC Wood')
        elif 'chân thép' in temp:
            lst.append('Steel')
        return lst
        continue
    
df['Material'] = df.apply(add_material, axis = 1)

Lists in 'Material' column don't contain second material.I want a second columns like this:

cols_2 = {'Material': [['Synthetic Leather', 'Oak'], ['Melamine coated MFC Wood', 'Steel']]}

Thank you.

CodePudding user response:

You were almost there. In your add_material function, you iter over ervery character in the product descritpion string, so you get no match.

import pandas as pd
data = {'Product Description': [': da tổng hợp, khung gỗ sồi canada.', ': gỗ mfc phủ melamine, chân thép sơn tĩnh']}
df = pd.DataFrame(data)

def add_material(x):
    lst = []
    if 'da tổng hợp' in x:
        lst.append('Synthetic Leather')
    if 'gỗ sồi' in x:
        lst.append('Oak')
    if 'gỗ mfc phủ melamine' in x:
        lst.append('Melamine coated MFC Wood')
    if 'chân thép' in x:
        lst.append('Steel')
    return lst

df['Material'] = df["Product Description"].apply(add_material)

And you get

>>> df
                         Product Description                           Material
0        : da tổng hợp, khung gỗ sồi canada.           [Synthetic Leather, Oak]
1  : gỗ mfc phủ melamine, chân thép sơn tĩnh  [Melamine coated MFC Wood, Steel]
  • Related