In Pandas, can you do groupby in on two different datasets?

2018-06-09 Thread C W
6 2 1 32 3.5 1112484819 2005-04-02 23:33:39 3 1 47 3.5 1112484727 2005-04-02 23:32:07 4 1 50 3.5 1112484580 2005-04-02 23:29:40 I'm trying two methods: Method 1 (makes sense) > ratings[['movieId', 'rating']].groupb

Re: Is it possible to use 'groupby' asynchronously?

2016-10-06 Thread Ian Kelly
It worked very well. >> >> Now I want to use it to process a database table. I can select the rows in >> the desired sequence with no problem. However, I am using asyncio, so I am >> reading the rows asynchronously. >> >> Before I spend hours trying to figure

Re: Is it possible to use 'groupby' asynchronously?

2016-10-06 Thread Chris Angelico
table. I can select the rows in > the desired sequence with no problem. However, I am using asyncio, so I am > reading the rows asynchronously. > > Before I spend hours trying to figure it out, can anyone confirm if this is > doable at all, or is groupby not designed for this. Most

Is it possible to use 'groupby' asynchronously?

2016-10-06 Thread Frank Millman
I am using asyncio, so I am reading the rows asynchronously. My 'reader' class has __aiter__() and __anext__() defined. If I pass the reader to groupby, I get the error message 'object is not iterable'. Before I spend hours trying to figure it out, can anyone confirm if

Re: Pandas GroupBy does not behave consistently

2016-05-15 Thread Michael Selik
On Sun, May 15, 2016 at 7:07 AM David Shi wrote: > Hello, Michael, > > Pandas GroupBy does not behave consistently. > > Last time, when we had conversation, I used grouby. It works well. > > Now, I thought to re-write the program, so that I can end up with a clean > scri

Pandas GroupBy does not behave consistently

2016-05-15 Thread David Shi via Python-list
Hello, Michael, Pandas GroupBy does not behave consistently. Last time, when we had conversation, I used grouby.  It works well. Now, I thought to re-write the program, so that I can end up with a clean script. But, the problem is that a lot of columns are missing after groupby application. Any

Re: groupby behaviour

2013-02-26 Thread Paul Rubin
t that situation it's simplest to just convert each group to a list for processing it: for k,g in groupby(...): gs = list(g) # do stuff with gs calling list(g) reads all the elements in the group, advancing groupby's internal iterator to the next group. Since you'

Re: groupby behaviour

2013-02-26 Thread andrea crotti
2013/2/26 Ian Kelly : > On Tue, Feb 26, 2013 at 9:27 AM, andrea crotti > wrote: >> So I was trying to use groupby (which I used in the past), but I >> noticed a very strange thing if using list on >> the result: > > As stated in the docs: > > ""&qu

Re: groupby behaviour

2013-02-26 Thread Ian Kelly
On Tue, Feb 26, 2013 at 9:27 AM, andrea crotti wrote: > So I was trying to use groupby (which I used in the past), but I > noticed a very strange thing if using list on > the result: As stated in the docs: """ The returned group is itself an iterator that shares the

Re: groupby - summing multiple columns in a list of lists

2011-05-17 Thread Peter Otten
What I would like to do is allow a tuple/list of index values, rather > than a single index value to be summed up, so you could say > group_results(mylist,[0],[1,2]) would return [(1, 5,7), (2, 7,9)] but > I'm struggling to do so, any thoughts? Cheers > > from itertools impor

groupby - summing multiple columns in a list of lists

2011-05-17 Thread Jackson
than a single index value to be summed up, so you could say group_results(mylist,[0],[1,2]) would return [(1, 5,7), (2, 7,9)] but I'm struggling to do so, any thoughts? Cheers from itertools import groupby as gb from operator import itemgetter as ig def group_results(table,keys,value

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Peter Otten
e following: > I have a list of dictionaries, something like > > [ { "a": 1, "b": 1, "c": 3 }, > { "a": 1, "b": 1, "c": 4 }, > ... > ] > > and I'd like to iterate through all items with, e.g., "a"

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
> Are you basically after this, then? > > for a, a_iter in groupby(my_list, itemgetter('a')): >print 'New A', a >for b, b_iter in groupby(a_iter, itemgetter('b')): >b_list = list(b_iter) >for p in [&#

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Ulrich Eckhardt
Nico Schlömer wrote: > So when I go like > > for item in list: > item[1].sort() > > I actually modify *list*? I didn't realize that; I thought it'd just > be a copy of it. No, I misunderstood your code there. Modifying the objects inside the list is fine, but I don't thing you do that, provi

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Jon Clements
On 4 May, 12:36, Nico Schlömer wrote: > > Does this example help at all? > > Thanks, that clarified things a lot! > > To make it easier, let's just look at 'a' and 'b': > > > my_list.sort( key=itemgetter('a','b','c&#x

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
st of dictionaries, something like >> >> [ { "a": 1, "b": 1, "c": 3 }, >>   { "a": 1, "b": 1, "c": 4 }, >>   ... >> ] >> >> and I'd like to iterate through all items with, e.g., "a":

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
> Does this example help at all? Thanks, that clarified things a lot! To make it easier, let's just look at 'a' and 'b': > my_list.sort( key=itemgetter('a','b','c') ) > for a, a_iter in groupby(my_list, itemgetter('a')):

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Jon Clements
em is the following: > I have a list of dictionaries, something like > > [ { "a": 1, "b": 1, "c": 3 }, >   { "a": 1, "b": 1, "c": 4 }, >   ... > ] > > and I'd like to iterate through all items wit

Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Ulrich Eckhardt
have a list of dictionaries, something like > > [ { "a": 1, "b": 1, "c": 3 }, > { "a": 1, "b": 1, "c": 4 }, > ... > ] > > and I'd like to iterate through all items with, e.g., "a":1. What I do

itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
"a": 1, "b": 1, "c": 3 }, { "a": 1, "b": 1, "c": 4 }, ... ] and I'd like to iterate through all items with, e.g., "a":1. What I do is sort and then groupby, my_list.sort( key=operator.itemgetter('a') ) my_l

Re: Pivot Table/Groupby/Sum question

2008-01-04 Thread petr . jakes . tpc
On Jan 4, 4:55 pm, [EMAIL PROTECTED] wrote: > Petr thanks so much for your input. I'll try to learnSQL, especially > if I'll do a lot of database work. > > I tried to do it John's way as en exercise and I'm happy to say I > understand a lot more. Basically I didn't realize I could nest > dictiona

Re: Pivot Table/Groupby/Sum question

2008-01-04 Thread patrick . waldo
Petr thanks so much for your input. I'll try to learn SQL, especially if I'll do a lot of database work. I tried to do it John's way as en exercise and I'm happy to say I understand a lot more. Basically I didn't realize I could nest dictionaries like db = {country:{genre:{sub_genre:3}}} and cal

Re: Pivot Table/Groupby/Sum question

2008-01-03 Thread petr . jakes . tpc
On Jan 3, 3:41 pm, [EMAIL PROTECTED] wrote: > Yes in the sense that the top part will have merged cells so that > Horror and Classics don't need to be repeated every time, but the > headers aren't the important part. At this point I'm more interested > in organizing the data itself and i can worry

Re: Pivot Table/Groupby/Sum question

2008-01-03 Thread patrick . waldo
Yes in the sense that the top part will have merged cells so that Horror and Classics don't need to be repeated every time, but the headers aren't the important part. At this point I'm more interested in organizing the data itself and i can worry about putting it into a new excel file later. -- h

Re: Pivot Table/Groupby/Sum question

2008-01-02 Thread petr . jakes . tpc
> So the data comes in as a long list. I'm dealing with some > information on various countries with 6 pieces of information to > pivot. Just to make it simple it's like a video store database. The > data is like [Country, Category, Sub Category, Film Title, Director, > Number of Copies]. data

Re: Pivot Table/Groupby/Sum question

2008-01-02 Thread patrick . waldo
Sorry for the delay in my response. New Year's Eve and moving apartment > - Where the data come from (I mean: are your data in Excel already > when you get them)? > - If your primary source of data is the Excel file, how do you read > data from the Excel file to Python (I mean did you solve this

Re: Pivot Table/Groupby/Sum question

2007-12-29 Thread petr . jakes . tpc
> Yes, I realize Excel has excellent support for pivot tables. However, > I hate how Excel does it and, for my particular excel files, I need > them to be formated in an automated way because I will have a number > of them over time and I'd prefer just to have python do it in a flash > than to do

Re: Pivot Table/Groupby/Sum question

2007-12-29 Thread patrick . waldo
On Dec 29, 3:00 pm, [EMAIL PROTECTED] wrote: > Patrick, > > in your first posting you are writing "... I'm trying to learn how to > make pivot tables from some excel sheets...". Can you be more specific > please? AFIK Excel offers very good support for pivot tables. So why > to read tabular data fr

Re: Pivot Table/Groupby/Sum question

2007-12-29 Thread petr . jakes . tpc
Patrick, in your first posting you are writing "... I'm trying to learn how to make pivot tables from some excel sheets...". Can you be more specific please? AFIK Excel offers very good support for pivot tables. So why to read tabular data from the Excel sheet and than transform it to pivot tabel

Re: Pivot Table/Groupby/Sum question

2007-12-29 Thread John Machin
On Dec 29, 11:51 am, [EMAIL PROTECTED] wrote: > John would you mind walking me through your class in normal speak? Yes. > I > only have a vague idea of why it works and this would help me a lot to > get a grip on classes and this sort of particular problem. It's about time you got a *concrete*

Re: Pivot Table/Groupby/Sum question

2007-12-28 Thread patrick . waldo
Petr, thanks for the SQL suggestion, but I'm having enough trouble in Python. John would you mind walking me through your class in normal speak? I only have a vague idea of why it works and this would help me a lot to get a grip on classes and this sort of particular problem. The next step is to

Re: Pivot Table/Groupby/Sum question

2007-12-28 Thread John Machin
On Dec 29, 9:58 am, [EMAIL PROTECTED] wrote: > What about to let SQL to work for you. The OP is "trying to learn how to make pivot tables from some excel sheets". You had better give him a clue on how to use ODBC on an "excel sheet" :-) [snip] > SELECT > NAME, > sum (AMOUNT) as TOTAL, > sum (case

Re: Pivot Table/Groupby/Sum question

2007-12-28 Thread petr . jakes . tpc
What about to let SQL to work for you. HTH Petr Jakes Tested on Python 2.5.1 8<-- #!/usr/bin/env python # -*- coding: utf-8 -*- import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() inputData=( ('Bob', 'Morn', 240), ('Bob', 'Aft', 300), ('Joe', 'Morn', 70), ('

Re: Pivot Table/Groupby/Sum question

2007-12-28 Thread patrick . waldo
Wow, I did not realize it would be this complicated! I'm fairly new to Python and somehow I thought I could find a simpler solution. I'll have to mull over this to fully understand how it works for a bit. Thanks a lot! On Dec 28, 4:03 am, John Machin <[EMAIL PROTECTED]> wrote: > On Dec 28, 11:4

Re: Pivot Table/Groupby/Sum question

2007-12-27 Thread John Machin
On Dec 28, 11:48 am, John Machin <[EMAIL PROTECTED]> wrote: > On Dec 28, 10:05 am, [EMAIL PROTECTED] wrote: > > > > If you have any ideas about how to solve this pivot table issue, which > > seems to be scant on Google, I'd much appreciate it. I know I can do > > this in Excel easily with the auto

Re: Pivot Table/Groupby/Sum question

2007-12-27 Thread John Machin
On Dec 28, 10:05 am, [EMAIL PROTECTED] wrote: > On Dec 27, 10:59 pm, John Machin <[EMAIL PROTECTED]> wrote: > > > On Dec 28, 4:56 am, [EMAIL PROTECTED] wrote: > > > > from itertools import groupby > > > You seem to have overlooked this important sentence in

Re: Pivot Table/Groupby/Sum question

2007-12-27 Thread patrick . waldo
On Dec 27, 10:59 pm, John Machin <[EMAIL PROTECTED]> wrote: > On Dec 28, 4:56 am, [EMAIL PROTECTED] wrote: > > > from itertools import groupby > > You seem to have overlooked this important sentence in the > documentation: "Generally, the iterable needs to alr

Re: Pivot Table/Groupby/Sum question

2007-12-27 Thread John Machin
On Dec 28, 4:56 am, [EMAIL PROTECTED] wrote: > from itertools import groupby You seem to have overlooked this important sentence in the documentation: "Generally, the iterable needs to already be sorted on the same key function" -- http://mail.python.org/mailman/listinfo/python-list

Pivot Table/Groupby/Sum question

2007-12-27 Thread patrick . waldo
ow to sum these values because of the iteration. I always get an error like: TypeError: iteration over non- sequence Here's the code: from itertools import groupby data = [['Bob', 'Morn', 240],['Bob', 'Aft', 300],['Joe', 'Morn', 70],

Re: groupby() seems slow

2007-10-16 Thread Raymond Hettinger
On Oct 15, 8:02 pm, 7stud <[EMAIL PROTECTED]> wrote: > t = timeit.Timer("test3()", "from __main__ import test3, key, data") > print t.timeit() > t = timeit.Timer("test1()", "from __main__ import test1, data") > print t.timeit() > > -

Re: groupby() seems slow

2007-10-15 Thread George Sakkis
On Oct 15, 11:02 pm, 7stud <[EMAIL PROTECTED]> wrote: > I'm applying groupby() in a very simplistic way to split up some data, > but when I timeit against another method, it takes twice as long. The > following groupby() code groups the data between the "" strings

groupby() seems slow

2007-10-15 Thread 7stud
I'm applying groupby() in a very simplistic way to split up some data, but when I timeit against another method, it takes twice as long. The following groupby() code groups the data between the "" strings: data = [ "1.5","","2.5","3.5"

Re: How to do this with groupby (or otherwise)? (Was: iterblocks cookbook example)

2007-06-04 Thread Gerard Flanagan
rit, it would be fun to compare several different > > approaches to the same problem using re.finditer, itertools.groupby, > > or the tokenize module. To get the ball rolling, here is one variant: > > > from itertools import groupby > > > def blocks(s, start, end): > >

How to do this with groupby (or otherwise)? (Was: iterblocks cookbook example)

2007-06-04 Thread Gerard Flanagan
or the tokenize module. To get the ball rolling, here is one variant: > > from itertools import groupby > > def blocks(s, start, end): > def classify(c, ingroup=[0], delim={start:2, end:3}): > result = delim.get(c, ingroup[0]) > ingroup[0] = result in (1, 2)

Re: groupby and itemgetter

2006-10-06 Thread Steven Bethard
Roman Bertle wrote: > Hello, > > there is an example how to use groupby in the itertools documentation > (http://docs.python.org/lib/itertools-example.html): > > # Show a dictionary sorted and grouped by value >>>> from operator import itemgetter >>>>

groupby and itemgetter

2006-10-06 Thread Roman Bertle
Hello, there is an example how to use groupby in the itertools documentation (http://docs.python.org/lib/itertools-example.html): # Show a dictionary sorted and grouped by value >>> from operator import itemgetter >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) >>> di

Re: "groupby" is brilliant!

2006-06-14 Thread Alex Martelli
James Stroud <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > James Stroud <[EMAIL PROTECTED]> wrote: > >... > > > >>def doit(rows, doers, i=0): > >> for r, alist in groupby(rows, itemgetter(i)): > >> if len(doers) >

Re: "groupby" is brilliant!

2006-06-14 Thread James Stroud
Alex Martelli wrote: > James Stroud <[EMAIL PROTECTED]> wrote: >... > >>def doit(rows, doers, i=0): >> for r, alist in groupby(rows, itemgetter(i)): >> if len(doers) > 1: >> doit(alist, doers[1:], i+1) >> doers[0](r) > >

Re: "groupby" is brilliant!

2006-06-14 Thread James Stroud
Alex Martelli wrote: > James Stroud <[EMAIL PROTECTED]> wrote: >... > >>def doit(rows, doers, i=0): >> for r, alist in groupby(rows, itemgetter(i)): >> if len(doers) > 1: >> doit(alist, doers[1:], i+1) >> doers[0](r) > >

Re: "groupby" is brilliant!

2006-06-14 Thread Alex Martelli
James Stroud <[EMAIL PROTECTED]> wrote: ... > def doit(rows, doers, i=0): >for r, alist in groupby(rows, itemgetter(i)): > if len(doers) > 1: >doit(alist, doers[1:], i+1) > doers[0](r) Isn't this making N useless slices (thus copies, for m

Re: "groupby" is brilliant!

2006-06-14 Thread Alex Martelli
Frank Millman <[EMAIL PROTECTED]> wrote: > Benji York wrote: > > Frank Millman wrote: > > > reader = csv.reader(open('trans.csv', 'rb')) > > > rows = [] > > > for row in reader: > > > rows.append(row) > > > > Why do you create a list of rows instead of just iterating over the > > reader direct

Re: "groupby" is brilliant!

2006-06-13 Thread Frank Millman
Benji York wrote: > Frank Millman wrote: > > reader = csv.reader(open('trans.csv', 'rb')) > > rows = [] > > for row in reader: > > rows.append(row) > > Why do you create a list of rows instead of just iterating over the > reader directly? > -- > Benji York A - didn't think of it - good idea

Re: "groupby" is brilliant!

2006-06-13 Thread Paul McGuire
John Machin" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > On 14/06/2006 8:38 AM, Robert Kern wrote: > > Gary Herron wrote: > >> John Machin wrote: > >> > >>> On 13/06/2006 6:28 PM, Paul McGuire wrote: > >>> > &

Re: "groupby" is brilliant!

2006-06-13 Thread John Machin
On 14/06/2006 8:38 AM, Robert Kern wrote: > Gary Herron wrote: >> John Machin wrote: >> >>> On 13/06/2006 6:28 PM, Paul McGuire wrote: >>> >>>> (Oh, and I like groupby too! Combine it with sort to quickly create >>>> histograms.) &g

Re: "groupby" is brilliant!

2006-06-13 Thread Robert Kern
Gary Herron wrote: > John Machin wrote: > >>On 13/06/2006 6:28 PM, Paul McGuire wrote: >> >>>(Oh, and I like groupby too! Combine it with sort to quickly create >>>histograms.) >>> >>># tally a histogram of a list of values from 1-10 &g

Re: "groupby" is brilliant!

2006-06-13 Thread John Machin
On 14/06/2006 8:06 AM, Gary Herron wrote: > John Machin wrote: >> On 13/06/2006 6:28 PM, Paul McGuire wrote: >> >> >>> (Oh, and I like groupby too! Combine it with sort to quickly create >>> histograms.) >>> >>> # tally a histogram of a

Re: "groupby" is brilliant!

2006-06-13 Thread Gary Herron
John Machin wrote: > On 13/06/2006 6:28 PM, Paul McGuire wrote: > > >> (Oh, and I like groupby too! Combine it with sort to quickly create >> histograms.) >> >> # tally a histogram of a list of values from 1-10 >> dataValueRange = range(1,11) >> d

Re: "groupby" is brilliant!

2006-06-13 Thread John Machin
On 13/06/2006 6:28 PM, Paul McGuire wrote: > (Oh, and I like groupby too! Combine it with sort to quickly create > histograms.) > > # tally a histogram of a list of values from 1-10 > dataValueRange = range(1,11) > data = [random.choice(dataValueRange) for i in xrange(1)

Re: "groupby" is brilliant!

2006-06-13 Thread Jon Clements
Not related to itertools.groupby, but the csv.reader object... If for some reason you have malformed CSV files, with embedded newlines or something of that effect, it will raise an exception. To skip those, you will need a construct of something like this: raw_csv_in = file('filenamehere.csv') fo

Re: "groupby" is brilliant!

2006-06-13 Thread James Stroud
w with the saved >> values, and taking action if the values differ. The more break fields >> there are, the fiddlier it gets. >> >> I was going to do the same in python, but then I vaguely remembered >> reading about 'groupby'. It took a little while to figure

Re: "groupby" is brilliant!

2006-06-13 Thread James Stroud
; there are, the fiddlier it gets. > > I was going to do the same in python, but then I vaguely remembered > reading about 'groupby'. It took a little while to figure it out, but > once I had cracked it, it transformed the task into one of utter > simplicity. > > H

Re: "groupby" is brilliant!

2006-06-13 Thread Benji York
Frank Millman wrote: > reader = csv.reader(open('trans.csv', 'rb')) > rows = [] > for row in reader: > rows.append(row) Why do you create a list of rows instead of just iterating over the reader directly? -- Benji York -- http://mail.python.org/mailman/listinfo/python-list

Re: "groupby" is brilliant!

2006-06-13 Thread geskerrett
Frank; I would just like to thank-you for this timely post. I am working on a reporting project that needed "groupby" functionality and I was going to sit down this morning to rework some "very ugly code" into some "not quite so ugly code". Your post got me pointed t

Re: "groupby" is brilliant!

2006-06-13 Thread Frank Millman
Paul McGuire wrote: > > > > reader = csv.reader(open('trans.csv', 'rb')) > > rows = [] > > for row in reader: > > rows.append(row) > > > > This is untested, but you might think about converting your explicit "for... > append" loop into either a list comp, > > rows = [row for row in reader]

Re: "groupby" is brilliant!

2006-06-13 Thread Paul McGuire
row in reader] or just a plain list constructor: rows = list(reader) Neh? -- Paul (Oh, and I like groupby too! Combine it with sort to quickly create histograms.) # tally a histogram of a list of values from 1-10 dataValueRange = range(1,11) data = [random.choice(dataValueRange) for i in xran

Re: "groupby" is brilliant!

2006-06-13 Thread vpr
, the fiddlier it gets. > > I was going to do the same in python, but then I vaguely remembered > reading about 'groupby'. It took a little while to figure it out, but > once I had cracked it, it transformed the task into one of utter > simplicity. > > Here is an exam

"groupby" is brilliant!

2006-06-12 Thread Frank Millman
about 'groupby'. It took a little while to figure it out, but once I had cracked it, it transformed the task into one of utter simplicity. Here is an example. Imagine a transaction file sorted by branch, account number, and date, and you want to break on

Re: groupby

2006-05-27 Thread Paul McGuire
"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > So here's how to save the values from the iterators while iterating over the > groupby: > > >>> m = [(x,list(y)) for x,y in groupby([1, 1, 1, 2, 2, 3])] > >>> m

Re: groupby

2006-05-22 Thread Paul McGuire
"Bryan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > George Sakkis wrote: > > "The returned group is itself an iterator that shares the underlying > > iterable with groupby(). Because the source is shared, when the groupby > > object is

Re: groupby

2006-05-22 Thread Bryan
George Sakkis wrote: > Bryan wrote: > >> can some explain why in the 2nd example, m doesn't print the list [1, 1, 1] >> which i had expected? >> >> >> >>> for k, g in groupby([1, 1, 1, 2, 2, 3]): >> ... print k, list(g) >>

Re: groupby

2006-05-22 Thread George Sakkis
Bryan wrote: > can some explain why in the 2nd example, m doesn't print the list [1, 1, 1] > which i had expected? > > > >>> for k, g in groupby([1, 1, 1, 2, 2, 3]): > ... print k, list(g) > ... > 1 [1, 1, 1] > 2 [2, 2] > 3 [3] > > > >

groupby

2006-05-22 Thread Bryan
can some explain why in the 2nd example, m doesn't print the list [1, 1, 1] which i had expected? >>> for k, g in groupby([1, 1, 1, 2, 2, 3]): ... print k, list(g) ... 1 [1, 1, 1] 2 [2, 2] 3 [3] >>> m = list(groupby([1, 1, 1, 2, 2, 3])) >>> m [(1, )