Skip to content

Instantly share code, notes, and snippets.

@chenghan
Last active February 5, 2022 20:17
Show Gist options
  • Save chenghan/7456549 to your computer and use it in GitHub Desktop.
Save chenghan/7456549 to your computer and use it in GitHub Desktop.
Instructor code that was shown on screen
import sys
salesTotal = 0
oldKey = None
for line in sys.stdin:
data = line.strip().split("\t")
if len(data) != 2:
# Something has gone wrong. Skip this line.
continue
thisKey, thisSale = data
if oldKey and oldKey != thisKey:
print oldKey, "\t", salesTotal
oldKey = thisKey
salesTotal = 0
oldKey = thisKey
salesTotal += float(thisSale)
if oldKey != None:
print oldKey, "\t", salesTotal
@HabibBG88
Copy link

i have implanted the code and as an output i find this
newyork 28
amazon 22
washdc 1
i wander why the tab doesnt work and the number are not in the same line thanks

@yvonnechanlove97
Copy link

Can I use groupby function in Pandas? That was my first thought

@zhujunqing1996
Copy link

I think this is probably how the groupby function in pandas works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment