1/6
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
import pandas as pd
earnings_2022 = chase_2022[(chase_2022['Description'].str.contains('PAYROLL'))].groupby(chase_2022['Posting Date'].dt.month)['Amount'].sum().round(0).reset_index(name = 'Income').rename(columns = {'Posting Date':'Month'})
chase_2022[(chase_2022[‘Description’].str.contains(‘PAYROLL’))] searches for instances where the substring payroll is located in description. . ].groupby(chase_2022[‘Posting Date’].dt.month)[‘Amont’].sum(). calculates the total amount per month. .reset_index(name = ‘Income’) renames the amount column to income and round(0) rounds the values in income to the nearest integer. Posing date is renamed to month. Essentially, this line calculates the total monthly amount for instances where the substring payroll is found in description and assigns the result to a dataframe called earnings_2022.
import pandas as pd
expenses_2022_1 = chase_2022[(chase_2022['Amount'] < 0)].groupby(chase_2022['Posting Date'].dt.month)['Amount'].sum().abs().round(0).reset_index().rename(columns = {'Posting Date':'Month'})
chase_2022[(chase_2022[‘Amount’] < 0)] returns instances where the values in Amount is less than 0. ].groupby(chase_2022[‘Posting Date’].dt.month)[‘Amount’].sum() calculates the total amount per month by extracting the month from posting date with dt.month.. .abs().round(0) returns positive values with abs and rounds the values in amount to the nearest integer. Posting date is renamed month. Essentially, this calculates the total monthly amount by for instances where the values in amount are less than 0 while returning the absolute value. The result is assigned to a dataframe called expenses_2022_1.
import pandas as pd
expenses_2022_2 = capitol_2022.groupby(capitol_2022['Posted Date'].dt.month)['Debit'].sum().round(0).reset_index().rename(columns = {'Posted Date':'Month'})
capitol_2022.groupby(capitol_2022[‘Posted Date’].dt.month)[‘Debit’].sum().round() calculates the total values in debit per month and rounds the results to the nearest integer. Posted date is renamed month. Essentially, this line calculates the total values in Debit per month and assigned it to a dataframe called expenses_2022_2.
import pandas as pd
expenses_2022 = expenses_2022.groupby('Month').apply(lambda x: x['Amount'] + x['Debit']).reset_index(name = 'Expenses')
expenses_2022 = expenses_2022[['Month', 'Expenses']]
expenses_2022.groupby(‘Month’).apply(lambda x: x[‘Amount’] + x[‘Debit’] uses a combination of lambda and groupby to calulate the sum of the amount and debit per month. groupby(‘Month’) groups the dataframe by month and .apply(lambda x: x[‘Amount’] + x[‘Debit’] sums the values in amount and debit where x represents each month’s group. reset_index(name = ‘Expenses’) renames the result expenses. expenses_2022[[‘Month’, ‘Expenses’]] only returns month and expenses and assigns the result to a dataframe called expenses_2022.