0

Is there a faster way to generate all permutations for all rows of a pandas DataFrame (and get it back as a dataframe)?

My current approach looks like this:

dataframe = pd.DataFrame(columns=["RX1", "RX2", "RX3", "RX4", "module"])
for idx, row in df.iterrows():
    vals = row.values
    for permutation in itertools.permutations(vals[0:4], 4):
        data = np.stack(permutation)
        dataframe.loc[len(dataframe)] = {
            "RX1": data[0], "RX2": data[1], "RX3": data[2], "RX4": data[3],
            "module": df.iloc[0]["module"]
        }

But it takes quite a bit of time. Is there a faster way to do this?

binaryBigInt
  • 1,526
  • 2
  • 18
  • 44
  • what is `df` in `df.iterrows()`? can you give a sample? – Ben.T Jul 29 '23 at 19:20
  • Try looking at [Create a dataframe of permutations in pandas from list](https://stackoverflow.com/questions/45672342/create-a-dataframe-of-permutations-in-pandas-from-list). – Alias Cartellano Jul 29 '23 at 21:10
  • @Ben.T It has 4 columns and each one contains an array of length 128 – binaryBigInt Jul 30 '23 at 05:20
  • You should add the values in a 5 lists and then create the dataframe from that. `dataframe.loc` should be very slow in this case because it will certainly create new growing columns internally resulting in a quadratic execution, not to mention `loc` is often considered as being slow. – Jérôme Richard Jul 30 '23 at 23:06

0 Answers0