col != ['SourceFile','Label'] df['FileDescription']=df[col].apply(lambda row:'_'.join(row.values.astype(str)),axis=1)
I want to combine the elements in all columns except two columns, ‘SourceFile’ and ‘Label’. I tried the above code. Which resulted in value error. There is so many columns. So I can’t use
col=['SourceFile','AggregationType','APP14Flags0','APP14Flags1','Application','ArchivedFileName','Artist',.....] df['FileDescription']=df[col].apply(lambda row:'_'.join(row.values.astype(str)),axis=1)
Advertisement
Answer
col != ['SourceFile','Label']
is syntactically wrong and it gives NameError not the ValueError.
First get the columns you don’t want and convert it to set.
col = set(['SourceFile','Label'])
Now get all columns as set:
allCols = set(df.columns.to_list())
Finally take the set difference and assign back as a list:
cols = list(set.difference(allCols, col))
Now you can use aggregate method:
df[col].astype(str).agg('_'.join)
See the sample execution:
df 0 1 2 3 4 5 6 7 8 9 0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 1 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 2 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 3 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 4 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 col= set([0]) allCols = set(df.columns.to_list()) col = list(set.difference(allCols, col)) df[col].astype(str).agg('_'.join, axis=1) 0 1.0_2.0_3.0_4.0_5.0_6.0_7.0_8.0_9.0 1 2.0_3.0_4.0_5.0_6.0_7.0_8.0_9.0_10.0 2 3.0_4.0_5.0_6.0_7.0_8.0_9.0_10.0_11.0 3 4.0_5.0_6.0_7.0_8.0_9.0_10.0_11.0_12.0 4 5.0_6.0_7.0_8.0_9.0_10.0_11.0_12.0_13.0 dtype: object