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.

1
  • 1
    What is the issue, exactly? Have you tried anything, done any research? As it stands, this is just a duplicate of stackoverflow.com/q/41585078/11301900. Commented Jan 22, 2020 at 0:02

2 Answers 2

4
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)
Sign up to request clarification or add additional context in comments.

2 Comments

Will this sample consecutive rows?
@Shamoon No it is random see doc for more detail pandas.dataframe.sample()
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"

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.