0
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.

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.