Skip to content

Instantly share code, notes, and snippets.

@dzakyputra
Last active June 17, 2020 00:01
Show Gist options
  • Save dzakyputra/82bff3ebf1298a31274e18960a45ae1a to your computer and use it in GitHub Desktop.
Save dzakyputra/82bff3ebf1298a31274e18960a45ae1a to your computer and use it in GitHub Desktop.
# Portfolio preprocessing
values = []
# Iterate the dataframe
for index, row in portfolio.iterrows():
# Set the default value
new_value = [0,0,0,0]
# If the value match with the channels, change it to 1
for item in row['channels']:
if item == 'web':
new_value[0] = 1
elif item == 'email':
new_value[1] = 1
elif item == 'mobile':
new_value[2] = 1
elif item == 'social':
new_value[3] = 1
values.append(new_value)
# Merge the current portfolio dataframe with the new one
portfolio = pd.concat([portfolio, pd.DataFrame(values, columns=['web', 'email', 'mobile', 'social'])], axis=1)
portfolio.drop(columns=['channels'], inplace=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment