kjell moens
kjell moens

Reputation: 39

Flattening index in python pandas

I am very new to the python and I have been playing around with Panda dataframes, but when I use a groupby, I am not longer able to iterate over the dataframes using the labels.

Can some help me ?

newDF=df[df['Currency'].str.contains(currency)&df['Description'].str.contains('fx')]
newDF=newDF.rename(index=str, columns={ "Paid": "Withdrawn"})

moneyWithdrawnByUserDF=pd.DataFrame(newDF.groupby(['FirstName'])[['Withdrawn']].sum())

for index,row in moneyWithdrawnByUserDF.iterrows():
  print row['FirstName']

The output/error I got is below :

Index([u'Email', u'FirstName', u'LastName', u'Owed', u'Withdrawn', u'UserId',

Traceback (most recent call last):

File "main.py", line 416, in <module>

sys.exit(main(sys.argv[1:]))

File "main.py", line 412, in main

parseGroups()

   u'Category', u'Description', u'Id', u'Currency', u'Cost', u'Details',

   u'GroupId'],

  dtype='object')

 File "main.py", line 45, in parseGroups

parseGroup(group) 

  File "main.py", line 81, in parseGroup processCurrencies(df)

  File "main.py", line 95, in processCurrencies processCurrency(df, currency)

  File "main.py", line 105, in processCurrency   moneyWithdrawnByUserDF=calculateMoneyWithdrawnByUser(df, currency)

 File "main.py", line 319, in calculateMoneyWithdrawnByUser

print row['FirstName']

 File "/usr/local/lib/python2.7/site-packages/pandas/core/series.py", line 601, in __getitem__

result = self.index.get_value(self, key)

 File "/usr/local/lib/python2.7/site-packages/pandas/core/indexes/base.py", line 2491, in get_value

  raise e1

 KeyError: 'FirstName'

Thank you

Upvotes: 0

Views: 180

Answers (1)

jezrael
jezrael

Reputation: 862591

I think you need change:

moneyWithdrawnByUserDF=pd.DataFrame(newDF.groupby(['FirstName'])[['Withdrawn']].sum())

by reset_index:

moneyWithdrawnByUserDF= newDF.groupby(['FirstName'])['Withdrawn'].sum().reset_index()

Or parameter as_index=False for DataFrame:

moneyWithdrawnByUserDF= newDF.groupby(['FirstName'], as_index=False)['Withdrawn'].sum()

Upvotes: 1

Related Questions