Use groupby and sample: import geopandas as gpd import os output_folder = r"/home/bera/Desktop/GIStest/csvs/" df = gpd.read_file(r"/home/bera/Desktop/GIStest/10k_points_wgs84.shp") #Calculate lat and long columns df["lat"] = df.apply(lambda x: x.geometry.y, axis=1) df["lon"] = df.apply(lambda x: x.geometry.x, axis=1) sample_size = 6 for dn, subframe in df.groupby("DN"): #For each DN value. #dn variable is now the value of dn, and subframe is a dataframe with all rows with that dn value print(dn) filename = f"DN_{dn}.csv" #Create an output filename filename = os.path.join(output_folder, filename) subframe.sample(n=sample_size).to_csv(filename, sep=";") [![enter image description here][1]][1] [1]: https://i.sstatic.net/SDz9D.png