Answers for "how to read any random rows in csv"

1

pandas read_csv random rows

import pandas as pd
import numpy as np

filename = 'hugedatafile.csv'
nlinesfile = 10000000
nlinesrandomsample = 10000
lines2skip = np.random.choice(np.arange(1,nlinesfile+1), (nlinesfile-nlinesrandomsample), replace=False)
df = pd.read_csv(filename, skiprows=lines2skip)
Posted by: Guest on August-21-2020
0

how to read any random rows in csv

import pandas as pd

data = {
		'Name': ['A', 'B', 'C', 'D', 'E', 'F'],
		'Age': [10, 20, 30, 40, 50, 60]
        }

df = pd.DataFrame(data)
print(df.sample(n=2))   # where 2 is the number of random rows
Posted by: Guest on September-20-2021

Python Answers by Framework

Browse Popular Code Answers by Language