1

I need help framing a generic script that helps avoid the following kind of hard coding of strings. Thanks in advance!

    df['Visual Labels'] = df['Visual Labels'].apply(lambda x: ','.join(map(str, x)))
    df['CONTAINS_CATEGORY'] = df['CONTAINS_CATEGORY'].apply(lambda x: ','.join(map(str, x)))
    df['image_CONTAINS_CATEGORY'] = df['image_CONTAINS_CATEGORY'].apply(lambda x: ','.join(map(str, x)))


    # Combine features coming from text and image
    df['Categories'] = df['CONTAINS_CATEGORY']+','+df['image_CONTAINS_CATEGORY']
    df.drop(["CONTAINS_CATEGORY","image_CONTAINS_CATEGORY","CONTAINS_CONCEPT","image_CONTAINS_CONCEPT"],axis=1,inplace=True)
    
    # Strip the last comma in the string included while joining two feature columns
    df['Categories'] = df['Categories'].str.replace(", ",",").str.rstrip(",")
Neha Madhavan
  • 15
  • 1
  • 4
  • If your data structure has constant attribute ? if yes, you could build a dictionary for configuration and named tuple as an abstract layer – jossefaz Jul 06 '20 at 07:50

0 Answers0