Pandas General Ledger Tutorial

Day 3: Account types for the Income Statement


There are two kinds of income statements: One is for companies that sell services and one that sells merchandise. The sevice business utilizes only two types of accounts: Revenue, C4 and expenses D6. The difference between the revenue and the expenses is called the net profit.

The company that we are analyzing is a merchandising business: they sell Mens' clothing. The income statement for this kind of business has a section on cost of good sold, D5 Purchases and Merchndise Inventory D1I.

Revenue accounts

Key the following code into a new cell, save your application and run all cells to see the index number of all revenue accounts.

ledger_gbclass.groups["C4"]

Here is the output.



Expense Accounts

Key the following code into a new cell, save your application and run all cells to see expense accounts.

ledger_gbclass.groups["D6"]

Here is the output.



Preparing an Income Statement

First, let's get the total of the revenue accounts. To accomplish this we will iterate or loop thru all the accounts and get the balance of the accounts with the C4 Type.

We are trying to get the total revenue. We declare the variable and set it equal to zero.

Next we set up the loop to look at the length of each row. The colon, begins the statements inside the loop. After the colon, statements are indented four spaces.

The "i" in the equation is the index number. In the if statement, we are looking for all rows or records that are designated as "C4", reveune accounts.

The 1 signifys the second item in the row which is Type. Remember, start counting with 0. Zero is the account name.

Now we want to print out each row's account number - 0, then we want to print each amount of the accounts designated as "C4".

The last line within the loop gets a total of the account balance of all revenue accounts.


Click copy text button and paste the contents into a new frame. Save your application and run the cells. There is no output at this time.

Printing the total revenue


Click copy text button and paste the contents into a new frame. Save your application and run the cells.

Beginning Inventory

As part of determining the cost of good sold we need to find the beginning inventory of merchandise. Our Pandas DataFrame designates this type as "D1I".


Click copy text button and paste the contents into a new frame. Save your application There is no output.

Create a new cell and key in the following code to get a printout of the beginning inventory.

print("Beginning Inventory")
print(beginningInventory)

Calculating Purchases

Click copy text button and paste the contents into a new frame. Save your application. There is no output.

Calculate Cost of Merchandise available for sale


Click copy text button and paste the contents into a new frame. Save your application. Run it to find cost of merchanise available for sale.

The above code creates a new variable,"mdseAvail". To find this total we need to add the beginning inventory and purchases together and round off the amount to two decimal places.

Calculating gross profit

The ending inventory is determined either by taking a physical inventory of the goods in the store or finding it in a prepetual inventory application.

"cogs" stands for cost of goods sold and it is found by subtracting the ending inventory from the cost of merchandis available for sale.

Gross profit is determined by subtracting cost of goods sold from the revenue total.

Both totals are printed out in the code below.


Click copy text button and paste the contents into a new frame. Save your application. Run it to find cost of cost of goods sold and gross profit.

Calculating expenses

Now we need to get the total of the expenses.


Click copy text button and paste the contents into a new frame. Save your application. Run it to find total expenses.