'How can I set a random selection from all columns from a CSV file?
How can I set a random selection from all columns from a CSV file? My selection mode is only from one column, although I have set to use 4 columns. The original CSV file Comes with all these cards (7,8,9,10, J, Q, K, A). I use np.random.choice because the regular random gives first preference for numbers.
Thank you
CSV file
card_1 Card_2 Card_3 Card_4
0 J 7 A 8
1 J 7 A 8
2 J 7 A 8
3 J 7 A 8
4 J 7 A 8
import pandas as pd
import numpy as np
df = pd.read_csv('test.csv')
random_colnames = np.random.choice(df.columns,4, replace = False) # columns
for col in random_colnames:
for _ in range(1):
draw = np.random.choice(df[col],4) # cards
print(draw)
Results
[8 8 8 8]
[7 7 7 7]
['J' 'J' 'J' 'J']
['A' 'A' 'A' 'A']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
