Skip to content

Instantly share code, notes, and snippets.

@samirsaci
Created March 19, 2021 20:59
Show Gist options
  • Save samirsaci/ffda67c1a3b44d786a0675efe4cba640 to your computer and use it in GitHub Desktop.
Save samirsaci/ffda67c1a3b44d786a0675efe4cba640 to your computer and use it in GitHub Desktop.
def create_links(df_rec, df_ordbr, list_brand):
''' Create links dataframe '''
# Unique brands per order
df_source = pd.DataFrame(df_rec.groupby(['ORDER_NUMBER'])['BRAND'].unique())
df_source.columns = ['list_brand']
list_source, list_target, list_value = [], [], []
for br1 in list_brand:
for br2 in list_brand:
value = (df_ordbr[br1] * df_ordbr[br2]).sum()
if value > 0:
list_source.append(br1)
list_target.append(br2)
list_value.append(value)
# Build links dataframe
df_links = pd.DataFrame({
'source': list_source,
'target': list_target,
'value':list_value
})
# Mapping with Brands ID
dict_map = dict(zip(df_links['source'].unique(), [i for i in range(len(list_brand))]))
for col in ['source', 'target']:
df_links[col] = df_links[col].map(dict_map)
return df_links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment