Using a for loop for row slicing

17 views
Skip to first unread message

Christopher Turnbull

unread,
Mar 18, 2016, 9:51:21 AM3/18/16
to PANDA Project Users
Hello All,

I'm trying to... "slide a window" acorss my data set to split it up into a series of data sets, each of which has 4 of the form of the orignal rows.

E.g, a data set with 10 rows, I want 8 new tables of this form:
row 0,1,2,3
row 1,2,3,4
...
row 7,8,9,10

I tried doing it on a simple example with a for loop:


df
= pd.DataFrame(np.random.randn(10,6))

for x in range(0,7):
    pieces
= [df[x:x+4],]

When I try

pieces

only the last one comes up however
(that is, rows 7,8,9 and 10)

How would I do this then?

I've been told that 'using list comprehension' is a better way to do this. Is that right?

Mike Stucka

unread,
Mar 18, 2016, 10:26:13 AM3/18/16
to panda-pro...@googlegroups.com
Christopher,

I think you're trying to work with pandas, a package for Python, rather than PANDA, which is a Python implementation of a search engine.

All *that* said ... I haven't worked with pandas, but I think what you're doing is creating a list called pieces. But each time you're working with pieces, you're setting the list to the newest ... ?

Try something like
pieces = []
df = pd.DataFrame(np.random.randn(10,6))
for x in range(0,7):
     pieces.append([df[x:x+4]])



Mike



--
You received this message because you are subscribed to the Google Groups "PANDA Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to panda-project-u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
26.58584, -80.16876 icbm
“A computer lets you make more mistakes faster than any invention in human history, with the possible exceptions of handguns and tequila.” -- Mitch Ratcliffe
Reply all
Reply to author
Forward
0 new messages