vendredi 26 mai 2017

Random pairs of all values in a pandas dataframe

import pandas as pd

df = pd.DataFrame({ 'lat' : range(0,8),
                    'name' : ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b']})
df

Output is:

    lat name
0   0   a
1   1   a
2   2   a
3   3   a
4   4   b
5   5   b
6   6   b
7   7   b

Now, what I would like to do is within each name type, create random pairings and add them together. However, all rows need to be a part of a random pairing.

So the ideal output would look something like:

  name  pairing sum
0   a   0,3      3
1   a   2,1      3
2   b   6,4      10
3   b   7,5      12

However, it's important that no a's are paired with b's, and that all values are in exactly one pair.

How can I accomplish this?




Aucun commentaire:

Enregistrer un commentaire