-1

My code takes a bank statement from Excel and creates a dataframe that categorises each transaction based on description:

import pandas as pd
import openpyxl
import datetime as dt
import numpy as np

dff = pd.DataFrame({'Date': ['20221003', '20221005'],
                   'Tran Type': ['BOOK TRANSFER CREDIT', 'ACH DEBIT'],
                   'Debit Amount': [0.00, -220000.00],
                   'Credit Amount': [182.90, 0.0],
                   'Description': ['BOOK TRANSFER CREDIT FROM ACCOUNT 98743987', 'USREF2548 ACH OFFSET'],
                   'Amount': [-220000.00, 182.90]})

import re
dff['Category'] = dff['Description'].str.findall('Ref|BCA|Fund|Transfer', flags=re.IGNORECASE)

But this code will not work. Any ideas why?

pivotf = dff
pivotf = pd.pivot_table(pivotf, 
index=["Date"], columns="Category",
values=['Amount'],
margins=False, margins_name="Total")

The error message is TypeError: unhashable type: 'list'

When I change columns from "Category" to anything else, it works fine.

Thanks!

Tomalak2Pi
  • 27
  • 5
  • Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Nov 12 '22 at 00:23
  • Amended above as you advise. – Tomalak2Pi Nov 12 '22 at 08:46

1 Answers1

0

Add this line before executing (untested):

import numpy as np
dff['category'] = [x[0] if not x.isempty() else np.nan for x in dff['category']]

This will make sure your category is not a list (which can't be hashed).

ABC
  • 189
  • 9