Skip to main content
added 175 characters in body
Source Link
Bera
  • 77.9k
  • 14
  • 78
  • 188

Use groupbygroupby and samplesample:

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

If you want all samples in one file you can use concat:

samples = [] #A list to hold each sample data frame
for dn, subframe in df.groupby("DN"): #For each DN value.
    samples.append(subframe.sample(n=sample_size))
result = gpd.pd.concat(samples) 
#result.to_csv...

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

If you want all samples in one file you can use concat:

samples = [] #A list to hold each sample data frame
for dn, subframe in df.groupby("DN"): #For each DN value.
    samples.append(subframe.sample(n=sample_size))
result = gpd.pd.concat(samples) 
#result.to_csv...

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

If you want all samples in one file you can use concat:

samples = [] #A list to hold each sample data frame
for dn, subframe in df.groupby("DN"): #For each DN value.
    samples.append(subframe.sample(n=sample_size))
result = gpd.pd.concat(samples) 
#result.to_csv...
added 298 characters in body
Source Link
Bera
  • 77.9k
  • 14
  • 78
  • 188

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

If you want all samples in one file you can use concat:

samples = [] #A list to hold each sample data frame
for dn, subframe in df.groupby("DN"): #For each DN value.
    samples.append(subframe.sample(n=sample_size))
result = gpd.pd.concat(samples) 
#result.to_csv...

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

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

If you want all samples in one file you can use concat:

samples = [] #A list to hold each sample data frame
for dn, subframe in df.groupby("DN"): #For each DN value.
    samples.append(subframe.sample(n=sample_size))
result = gpd.pd.concat(samples) 
#result.to_csv...
Source Link
Bera
  • 77.9k
  • 14
  • 78
  • 188

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