vendredi 19 juillet 2019

obtaining random sample from a randomly generated dataframe in a dynamic way (not hard-coded)

I've written some code that will generate a dataframe of randomly generated indices (500 rows). However, the task is to obtain 5 randomly selected rows ONLY from the last 50 rows (from the randomly generated dataframe). I've managed to select 5 rows but the problem is that they are not specifically from the last 50 indices of the dataframe as required. Plus, you cannot use hard-coding.

I have tried the following code:

'' This code should select 5 rows only from the last 50 rows of the dataframe''

def randomly_generated_dataframe(self, df1, select=5, start_range=50, end_range=1):

   selected_indices = np.random.random_integers(len(df1.index[-start_range:]), len(df1.index[:-end_range]), select)
   return df1.loc[selected_indices]

'' This code should select 5 rows only from the last 50 rows of the dataframe''

def randomly_generated_dataframe(self, df1, select=5, start_range=50, end_range=1): 

    selected_indices = np.random.random_integers(len(df1.index[-start_range:-end_range]), select)
    return df1.loc[selected_indices]

I don't get any error message as such but the output is incorrect since the 5 selected rows seem to come from any part of the dataframe and not just from the last 50 rows as intended.




Aucun commentaire:

Enregistrer un commentaire