A quick way to find all pandas DataFrame permutations that preserves sorting?

I have a DataFrame, and I would like to find all of its permutations that perform simple upstream sorting on one of the columns. (There are many relationships.) For example, in the following DataFrame

df = pd.DataFrame({'name': ["Abe", "Bob", "Chris", "David", "Evan"], 
                   'age': [28, 20, 21, 22, 21]})

I would like to sort by age and receive orders ["Bob", "Chris", "Evan", "David", "Abe"]and ["Bob", "Evan", "Chris", "David", "Abe"].

I am new to python (and pandas) and am curious if there is an easy way to do this that I don't see.

Thank!

+4
source share
5 answers

, , ( itertools ):

In [11]: age = df.groupby("age")

:

In [12]: age.get_group(21)
Out[12]:
   age   name
2   21  Chris
4   21   Evan

In [13]: list(permutations(age.get_group(21).index))
Out[13]: [(2, 4), (4, 2)]

In [14]: [df.loc[list(p)] for p in permutations(age.get_group(21).index)]
Out[14]:
[   age   name
 2   21  Chris
 4   21   Evan,    age   name
 4   21   Evan
 2   21  Chris]

DataFrame, ( , , reset_index ... - ):

In [21]: [list(permutations(grp.index)) for (name, grp) in age]
Out[21]: [[(1,)], [(2, 4), (4, 2)], [(3,)], [(0,)]]

In [22]: list(product(*[(permutations(grp.index)) for (name, grp) in age]))
Out[22]: [((1,), (2, 4), (3,), (0,)), ((1,), (4, 2), (3,), (0,))]

:

In [23]: [sum(tups, ()) for tups in product(*[(permutations(grp.index)) for (name, grp) in age])]
Out[23]: [(1, 2, 4, 3, 0), (1, 4, 2, 3, 0)]

, loc ( ):

In [24]: [df.loc[list(sum(tups, ()))] for tups in product(*[list(permutations(grp.index)) for (name, grp) in age])]
Out[24]:
[   age   name
 1   20    Bob
 2   21  Chris
 4   21   Evan
 3   22  David
 0   28    Abe,    age   name
 1   20    Bob
 4   21   Evan
 2   21  Chris
 3   22  David
 0   28    Abe]

() :

In [25]: [list(df.loc[list(sum(tups, ())), "name"]) for tups in product(*[(permutations(grp.index)) for (name, grp) in age])]
Out[25]:
[['Bob', 'Chris', 'Evan', 'David', 'Abe'],
 ['Bob', 'Evan', 'Chris', 'David', 'Abe']]

. numpy pd.tools.util.cartesian_product. , muchness , ( , )...

+3

:

import pandas as pd
from itertools import permutations, product

df = pd.DataFrame({'name': ["Abe", "Bob", "Chris", "David",
                            "Evan","Ford","Giles","Ham"],
                   'age': [20, 20, 21, 22,
                           21, 21, 22, 22]})

dfg = df.groupby('age')
perms = {}
for k, v in dfg:
    perms[k] =  list(permutations(v.values))

print(perms)

perms - --21- .

+1

cphlewis, :

import pandas as pd
import itertools as it

df = pd.DataFrame({'name': ["Abe", "Bob", "Chris", "David",
                            "Evan", "Ford", "Giles", "Ham"],
                   'age': [20, 21, 20, 20,
                           24, 25, 25, 27]})

dfg = df.groupby('age')

permsAtEachAge = []
for age, people in dfg:
    permsAtEachAge.append(list(it.permutations(people.name.values)))

product = list(it.product(*permsAtEachAge))
overallPermutations = map(lambda x: list(it.chain(*x)), product)

Andy Hayden .

0

numpy

np.sort( []) [:: - 1]

-1
import pandas as pd

df.sort_values(by = 'age')
-1

All Articles