I have a variable in Pandas dataframe called "label" which contains multiple string values (for example: 'label1', "label2', 'label3'...).
label
label1
label1
label23
label3
label11
I output all unique values into a list and then create new variables
unique_labels = df['label'].unique()
for i in unique_labels: # create new single label variable holders
df[str(i)] = 0
Now I have
label label1 label2 .... label23
label1 0 0 0
label23 0 0 0
I want to assign corresponding value based on 'label' onto the new single label variables, as following
label label1 label2 .... label23
label1 1 0 0
label23 0 0 1
Here is my code
def single_label(df):
for i in range(len(unique_labels)):
if df['label'] == str(unique_labels[i]):
df[unique_labels[i]] == 1
df = df.applymap(single_label)
Getting this error
TypeError: ("'int' object is not subscriptable", 'occurred at index Unnamed: 0')