dartdog
dartdog

Reputation: 10862

Calling a function on a list with Python/Pandas

I have the following simple function:

def sumsubdesc(table,subs):
    H2=table[(table['SUBDIVISION']==subs)&(table['AREA']=='260B')]
    H3=H2[['LIST_PRICE','SOLD_PRICE']]
    H4=H3.describe()
    return H4 , subs

And the following List of subs:

subsl=['EAGLE POINT','HEATHERWOOD']

I want to call sumsubdesc and have it print the result of the function (which is the pandas describe) for each subdivision in the list so:

for subsm in subsl:
    sumsubdesc(table, subsm)
    print H4,subsm

Which gives:

          LIST_PRICE     SOLD_PRICE
 count     355.000000     166.000000
 mean   438701.030986  397962.518072
 std    116994.150714  106734.004085
 min    164900.000000  150200.000000
 25%    359450.000000  330375.000000
 50%    429900.000000  380000.000000
 75%    499900.000000  458986.500000
 max    873240.000000  898492.000000 EAGLE POINT
           LIST_PRICE     SOLD_PRICE
 count     355.000000     166.000000
 mean   438701.030986  397962.518072
 std    116994.150714  106734.004085
 min    164900.000000  150200.000000
 25%    359450.000000  330375.000000
 50%    429900.000000  380000.000000
 75%    499900.000000  458986.500000
 max    873240.000000  898492.000000 HEATHERWOOD

Note that the name Heatherwood and Eagle point does get passed through but they do not get used as ['SUBDIVISION'] for the table selection (line 2 of the function) which is why the data in the describe is the same in both blocks. I know I'm doing something wrong with scope but don't know what?

Upvotes: 0

Views: 133

Answers (1)

Phillip Cloud
Phillip Cloud

Reputation: 25662

Change

for subsm in subsl:
    sumsubdesc(table, subsm)
    print H4,subsm

to

for subsm in subsl:
    H4, subsm = sumsubdesc(table, subsm)
    print H4,subsm

Upvotes: 1

Related Questions