vendredi 18 décembre 2020

Combine two dataframes in Pandas to generate many to many relationship

I have two lists, say

customers = ['a', 'b', 'c']
accounts = [1, 2, 3, 4, 5, 6, 7, 8, 9]

What is the best way to generate a Pandas dataframe so:

  1. All customers and accounts are used
  2. There is a many to many relationship between customers and accounts (one customer 'may' have multiple accounts and an account 'may' be owned by multiple customers

Something like,

Customer Account
a 1
a 2
b 2
c 3
a 4
b 4
c 4
b 5
b 6
b 7
b 8
a 9

Since I am generating random data, in the worst case scenario, I can generate way too many accounts and discard the unused ones if the code is easier (essentially relaxing the requirement 1 above).




Aucun commentaire:

Enregistrer un commentaire