dimanche 26 septembre 2021

Get random number of rows from an excel sheet in a dictionary Python

I have a dictionary with name "new". I has around 200 key values naming "5,10,15...1000". Each Key has a dataframe with two columns (length varies for each key).

{'5':      C         V
 0     1.398926   3.284607
 1     1.399078   3.287659
 2     1.399078   3.290710
 3     1.399231   3.293152
 4     1.399384   3.295288
 ...        ...        ...
 7732  0.156128   4.247131
 7733  0.150513   4.246826
 7734  0.151444   4.246826
 7735  0.151993   4.246826
 7736  0.144974   4.246521

I want to extract random percentage of rows in same sequence (20% to 80% of total rows) from each key and store it in a another dictionary "Name".

random.seed(100)      # to get same random number always
Name = {}
for sheet in new:

k = round(random.uniform(0.2,0.8),2) #generate a random number between 0.2 and 0.8 
Name[sheet] = new[sheet].sample(frac=k, random_state=50)

I tried this. With this I am getting percentage of total rows but each row is being picked up from a random position. They are not in sequence as shown below.

{'5':     C          V
 6994  1.167145  4.249268
 1248  1.402283  3.587341
 3530  1.402588  3.820190
 4585  1.402283  3.962708
 4058  1.402283  3.894653
 ...        ...       ...
 6603  1.401367  4.211426
 1348  1.402435  3.599243
 6383  1.401672  4.196167
 7716  0.157791  4.247131
 5413  1.401978  4.063110

But I need the rows picked up to be in sequence. Please let me know how I can do that.




Aucun commentaire:

Enregistrer un commentaire