4

This may be a simple question, and I apologize if it's too simple. But I have some data in a CSV:

Date,Open,High,Low,Close,Adj Close,Volume
1993-01-29,43.968750,43.968750,43.750000,43.937500,26.453930,1003200
1993-02-01,43.968750,44.250000,43.968750,44.250000,26.642057,480500
1993-02-02,44.218750,44.375000,44.125000,44.343750,26.698507,201300
1993-02-03,44.406250,44.843750,44.375000,44.812500,26.980742,529400
1993-02-04,44.968750,45.093750,44.468750,45.000000,27.093624,531500
1993-02-05,44.968750,45.062500,44.718750,44.968750,27.074818,492100
1993-02-08,44.968750,45.125000,44.906250,44.968750,27.074818,596100
1993-02-09,44.812500,44.812500,44.562500,44.656250,26.886669,122100
....

I want to create a "training set", which is basically a random vector of 10 rows of data (I can figure out the normalizing, etc) randomly sampled from anywhere in the file. I think I'll have to use pandas to do the loading maybe?

If what I'm trying to ask is unclear, please add comments and I will adjust the question accordingly. Thank you.

Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 1
    What is the issue, exactly? Have you tried anything, done any research? As it stands, this is just a duplicate of https://stackoverflow.com/q/41585078/11301900. – AMC Jan 22 '20 at 00:02

2 Answers2

3
import pandas as pd

sample = pd.read_csv('myfile.csv').sample(n=10)

you should load the file only 1 time and then sample as you go:

df = pd.read_csv('myfile.csv')
sample1 = df.sample(n=10)
sample2 = df.sample(n=10)
Steven G
  • 16,244
  • 8
  • 53
  • 77
  • Will this sample consecutive rows? – Shamoon Jan 21 '20 at 23:30
  • @Shamoon No it is random see doc for more detail [`pandas.dataframe.sample()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sample.html#pandas-dataframe-sample) – Dishin H Goyani Jan 22 '20 at 03:58
0

To read csv, you need to import pandas.

Use this code

import pandas as pd
data = pd.read_csv("filename.csv")

Put the filename.csv in quotation marks. If your file is in a different folder, use the full path in quotations "C:/Users/user/Desktop/folder/file.csv"

Gerald Hoxha
  • 29
  • 1
  • 1
  • 6
  • 1
    _To read csv, you need to import pandas._ Assuming that you mean "In order to read a CSV file, you need to use Pandas", that's completely false. – AMC Jan 22 '20 at 00:04