vendredi 16 mars 2018

Random Sampling from a column several times in Python/Pandas

I am new to Pandas and Python. I will write my question over an example. I have a data such as

df = pd.DataFrame([[1, 2], [1, 3], [4, 6], [5,6], [7,8], [9,10], [11,12], [13,14]], columns=['A', 'B'])
df 
    A   B

0   1   2

1   1   3

2   4   6

3   5   6

4   7   8

5   9   10

6   11  12

7   13  14

I am taking 3 samples from both column.

x = df['A'].sample(n=3)
x = x.reset_index(drop=True)
x

0     7
1     9
2    11

y = df['B'].sample(n=3)
y = y.reset_index(drop=True)
y

0     6
1    12
2     2

I would like to do this taking sample(n=3) 10 times. I tried [y] * 10, it produces columns 10 times out of 6,12,2. I want to do this 10 times from main data.Then I would like to make a new data out of this new columns generated from A and B. I thought maybe I should write for loop but I am not so familiar with them.

Thanks for the helps.




Aucun commentaire:

Enregistrer un commentaire