Python Forum
How do I create permutations and combinations of a set of values?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I create permutations and combinations of a set of values?
#1
Hello everyone,
This might be a bit elaborate question so just giving you a heads up!

I have been trying to use dataframes to create the following combinations but have not been able to make much progress as I am relatively new to Python!

I have the following set of values using which I need to create 4 sets of permutations/combinations

Strike Premium

A 100
B 200
C 300

Set 1:
It will consist of an additional column called 'Trade' and look like this:

First all the original values are copied followed by the text 'Buy' in the 'Trade' column.

Strike Premium Trade
A 100 Buy
B 200 Buy
C 300 Buy

Secondly, the original values are again copied followed by the text 'Sell' in the 'Trade' column.

A 100 Sell
B 200 Sell
C 300 Sell

So the final output should look like this:
Strikes Premium Trade
A 100 Buy
B 200 Buy
C 300 Buy
A 100 Sell
B 200 Sell
C 300 Sell

Set 2:

I copy the output of Set 1 first and add another set of columns(Strikes, Premium & Trade). Then I iterate through all of the input data and enter each of the row values with 'Buy' marked against it in 'Trade' first and then repeat with 'Sell' in the Trade column. Then I perform the same operation for the next row of input data and so on until I get the following output:

Strike Premium Trade Strike Premium Trade
A 100 Buy A 100 Buy
B 200 Buy A 100 Buy
C 300 Buy A 100 Buy
A 100 Sell A 100 Buy
B 200 Sell A 100 Buy
C 300 Sell A 100 Buy
A 100 Buy A 100 Sell
B 200 Buy A 100 Sell
C 300 Buy A 100 Sell
A 100 Sell A 100 Sell
B 200 Sell A 100 Sell
C 300 Sell A 100 Sell
A 100 Buy B 200 Buy
B 200 Buy B 200 Buy
C 300 Buy B 200 Buy
A 100 Sell B 200 Buy
B 200 Sell B 200 Buy
C 300 Sell B 200 Buy
A 100 Buy B 200 Sell
B 200 Buy B 200 Sell
C 300 Buy B 200 Sell
A 100 Sell B 200 Sell
B 200 Sell B 200 Sell
C 300 Sell B 200 Sell
A 100 Buy C 300 Buy
B 200 Buy C 300 Buy
C 300 Buy C 300 Buy
A 100 Sell C 300 Buy
B 200 Sell C 300 Buy
C 300 Sell C 300 Buy
A 100 Buy C 300 Sell
B 200 Buy C 300 Sell
C 300 Buy C 300 Sell
A 100 Sell C 300 Sell
B 200 Sell C 300 Sell
C 300 Sell C 300 Sell

Similarly I create Set 3 and Set 4 as well where each row contains 3 & 4 sets of Strike/Premium/Trade value-combinations respectively.

This has not possibly been my best attempt at explanation, so please feel free to let me know if you have any questions.

I would really appreciate any help to help solve this issue.

Thanks in advance!
Reply
#2
look at itertools module
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Sep-11-2020, 08:38 AM)buran Wrote: look at itertools module

Thanks for the headsup! Checking out the tutorials now!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a dataframe from timestamp values MohanReddy 5 976 Mar-20-2019, 09:11 AM
Last Post: samsonite
  Improve segmentation : Create sphere brush to edit voxels values in scanners image wi LauraLaura 1 917 Jul-19-2018, 12:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020