Coder Social home page Coder Social logo

Comments (4)

rhshadrach avatar rhshadrach commented on June 3, 2024 1

I'm not sure if there will be much interest in this feature.

Agreed @Aloqeely - CSV reading in pandas is already very complex and I think we would like to simplify it where possible. This feature seems too niche to me to have it supported directly in pandas.

If I have a large dataset with 100K rows, and I want to select 1000 rows at random, I first need to load the whole dataset into my RAM

How does one determine which 1000 rows to choose without knowing the total number of rows? Wouldn't that require reading the entire file anyways?

from pandas.

mroeschke avatar mroeschke commented on June 3, 2024 1

Thanks for the request but it seems like there's not much interest in this feature so closing

from pandas.

twoertwein avatar twoertwein commented on June 3, 2024

I think you can use skiprows for this (I did not test it)

def selectFirstNRandom(size: int):
    selected_count = 0
    def skip(index: int):
        if selected_count >= size:
            return True
        skip_row = random.random() > 0.5
        if not skip_row:
            selected_count += 1
         return skip_row
    return skip

df = pd.read_csv('your_file.txt', skiprows=selectFirstNRandom(100))

from pandas.

Aloqeely avatar Aloqeely commented on June 3, 2024

I'm not sure if there will be much interest in this feature.

@twoertwein's skiprows solution probably works, but I think using chunksize and iterator is cleaner, it's less random though.

it = pd.read_csv("test.csv", chunksize=1000, iterator=True)

chunk1 = it.get_chunk()  # returns DataFrame with first 1000 lines
chunk2 = it.get_chunk()  # returns DataFrame with the next thousand lines

For more information on the chunksize and iterator parameters, you can see the IO Tools user guide

from pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.