search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

# When file contains a header row

Consider the following `my_data.txt` file:

``` A,B,C1,2,34,5,67,8,9 ```

To read `n` random lines using `read_csv(~)` in Pandas.

``` import randomdef get_num_lines(fname): with open(fname) as f: for i, _ in enumerate(f): pass return i + 1num_lines = get_num_lines("my_data.txt") - 1# How many randomn rows do you want?sample_size = 2rows_to_skip = random.sample(range(1,num_lines), num_lines-sample_size)df = pd.read_csv("my_data.txt", skiprows=rows_to_skip)df A B C0 1 2 31 7 8 9 ```

Note the following:

• we first start by fetching the total number of lines in the file. Since we have a header row in our file, we subtract the number by `1`. In this case, `num_lines=3`.

• we then use `random.sample(~)` method to randomly get the row numbers to skip.

• the first argument is the values to randomly select from. In this case, since `num_lines=3`, random integers between `1` (inclusive) and `3` (inclusive) is chosen. We used `range(1,_)` because the first line of the file is for column labels, and so we don't want to skip this row. In this case, it turned out that `rows_to_skip=`, which means that the second row is skipped.

• the second argument is the number of random integers you want.

# When file does not contain a header row

Consider the following `my_data.txt` file:

``` 1,2,34,5,67,8,9 ```

To read `n` random lines using `read_csv(~)`:

``` import randomdef get_num_lines(fname): with open(fname) as f: for i, _ in enumerate(f): pass return i + 1num_lines = get_num_lines("my_data.txt")# How many random rows do you want?sample_size = 2rows_to_skip = random.sample(range(num_lines), num_lines-sample_size)df = pd.read_csv("my_data.txt", skiprows=rows_to_skip, header=None)df 0 1 20 4 5 61 7 8 9 ```

Note the following:

• we first start by fetching the total number of lines in the file. In this case, `num_lines=3`.

• we then use `random.sample(~)` method to randomly get the row numbers to skip. In this case, it turns out that `rows_to_skip=`.

thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!