mercredi 11 novembre 2020

Generating random sample from csv file, whilst skipping rows with certain values

I'm trying to generate a random subsample of 5000 rows from a .csv file containing tens of thousands of rows. The df contains two columns: 'JPG' and 'NAME'.

I have generated a random subsample with the following code:

import pandas as pd

file = pd.read_csv(r'C:\filepath\data.csv', usecols = [7, 8])

sample = file.sample(n=5000)
print(sample)

However, now I wish to do the same, but including a for-loop that can do so whilst skipping any rows with the string 't3' in the 'NAME' column.

Here's where I'm at, but stuggling to make it work:

import pandas as pd

file = pd.read_csv(r'C:\filepath\data.csv', usecols = [7, 8])

sample = file.sample(n=5000)
for num in sample:
    if sample.loc[sample['NAME'] == 't3']:
        continue
print(sample)

Any help on this would be greatly appreciated.

Thanks, R

Aucun commentaire:

Enregistrer un commentaire