2

I have a list named list1

  list1 = ['Banana','Apple','Pear','Strawberry','Muskmelon','Apricot','Peach','Plum','Cherry','Blackberry','Raspberry','Cranberry','Grapes','Greenapple','Kiwi','Watermelon','Orange','Lychee','Custardapples','Jackfruit','Pineapple','Mango']

I want to form a df with specific columns and random data from list1

Eg:

        a          b       c            d            e         f
   0  Banana    Orange   Lychee     Custardapples Jackfruit  Pineapple
   1   Apple    Pear   Strawberry   Muskmelon    Apricot    Peach
   2  Raspberry Cherry  Plum           Kiwi        Mango   Blackberry

A structure something like this but with random data from list1? There can't be any duplicate/repeated values present.

2
  • Could you please add sample items in each column from your list, so it will be more clear? Commented Dec 21, 2019 at 17:22
  • updated with random data for reference Commented Dec 21, 2019 at 17:28

1 Answer 1

4

If every item from the list can end up everywhere in the DataFrame you could write:

pd.DataFrame(np.random.choice(list1, 3*6, replace=False).reshape(3, 6), columns=list("abcdef"))

Out: 
        a           b           c          d              e          f
0  Lychee       Peach     Apricot       Pear           Plum     Grapes
1  Cherry   Jackfruit  Blackberry  Cranberry           Kiwi      Apple
2  Orange  Greenapple  Watermelon     Banana  Custardapples  Raspberry

The replace-parameter in np.random.choice() is True by default, so for unique values you need to set it to False.

Sign up to request clarification or add additional context in comments.

3 Comments

Great, it's in a single line.
Thanks @Darkonaut. what should be done if item can be present once in a df. In above example item 'Watermelon' came thrice. How to make it only once.
@Zanthoxylumpiperitum Set choice(...replace=False) then, it's True by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.