Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75358

Re: Load a CSV with different row lengths

X-Received by 10.43.64.202 with SMTP id xj10mr2680661icb.0.1406696423209; Tue, 29 Jul 2014 22:00:23 -0700 (PDT)
X-Received by 10.50.221.39 with SMTP id qb7mr821993igc.0.1406696423122; Tue, 29 Jul 2014 22:00:23 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!enother.net!enother.net!news.glorb.com!h18no10260020igc.0!news-out.google.com!px9ni0igc.0!nntp.google.com!h18no10260015igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Tue, 29 Jul 2014 22:00:22 -0700 (PDT)
In-Reply-To <mailman.12416.1406639326.18130.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=79.183.177.9; posting-account=uo-8fwoAAACKsFzFX78JHudx1V7WDXZ0
NNTP-Posting-Host 79.183.177.9
References <CAKcMNB+pYdcETcCs=QEPhU-mLr3nwqX0T66_jT1zq-vgWGMeLg@mail.gmail.com> <CANc-5UxaZh6So8cy94FV17ic6MzFh0L5LZrk+3SZbyCPCtUUmQ@mail.gmail.com> <mailman.12416.1406639326.18130.python-list@python.org>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <b0b596fc-1e4c-49da-80e6-7095edef9ceb@googlegroups.com> (permalink)
Subject Re: Load a CSV with different row lengths
From Miki Tebeka <miki.tebeka@gmail.com>
Injection-Date Wed, 30 Jul 2014 05:00:23 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:75358

Show key headers only | View raw


Greetings,

> I should've mentioned that I want to import my csv as a data frame or numpy array or as a table.
If you know the max length of a row, then you can do something like:
    def gen_rows(stream, max_length):
        for row in csv.reader(stream):
            yield row + ([None] * (max_length - len(line))

    max_length = 10
    with open('data.csv') as fo:
        df = pd.DataFrame.from_records(gen_rows(fo, max_length))


HTH,
Miki

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Load a CSV with different row lengths Ryan de Vera <ryan.devera.03@gmail.com> - 2014-07-29 09:08 -0400
  Re: Load a CSV with different row lengths Miki Tebeka <miki.tebeka@gmail.com> - 2014-07-29 22:00 -0700
    Re: Load a CSV with different row lengths Peter Otten <__peter__@web.de> - 2014-07-30 10:16 +0200

csiph-web