Let's say I have a DataTable with ~50 rows (GetDataTable() on a list in SharePoint). I want to keep 10 random rows and forget about the rest. How can I achieve this?
Thanks in advance.
To remove rows at random without shuffling in Pandas DataFrame: Get an array of randomly selected row index labels. Use the drop(~) method to remove the rows.
You can use a Fisher/Yates shuffle (implementation by Skeet) on the collection of rows on the DataTable, then select the first 10.
var random10 = dataTable.Rows.OfType<DataRow>().Shuffle(new Random()).Take(10);
The DataTable contain a property Rows
that is a DataRowCollection
. You can access each of those rows by using index.
So you can get random number with Random
and get the data from the myTable.Rows[myRandomIndex]
.
Random random = new Random();
int randomNumber = random.Next(0, 50);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With