Create new pandas dataframe with calculated cols

Solution for Create new pandas dataframe with calculated cols
is Given Below:

I’m writing a Python program that tracks purchases of etf’s. The input method is already done and my data is stored in a CSV file. Now I want to create a history of my purchases to be able to see what the performance for each month looks like, for example:

p: date,amount
p1: 2020-01-12,23
p2: 2ß20-02-12, 34

history will have two rows:

p: date, amount
p1: 2020-01,23
p2: 2020-02,57 (23+34)

I want to create an algorithm which does the following:

  • search in dataframe raw data – done
  • make subset for user and one single etf – done
  • create new empty dataframe: history – done
  • get first row in subset (which I’m currently working on)
    • set date
      • select col date (format 2020-12-24)
      • extract substring year and write it to new col YEAR in history
      • extract substring month and write it to new col MONTH in history
    • calculate amount
      • select col amount
      • write it to new col amount in subset history
    • write new row to history
  • get second row in subset
    • set date (same as above)
    • calculate amount
      • get amount value of first row
      • calculate sum of amount in first and second row
    • write new row to history
  • repeat for complete subset
  • write new created dataframe history to a CSV file

So far, my code is:

import pandas as pd
#read csv
#print("read_csv")
csv_file = "../data/purchases.csv"
df = pd.read_csv(csv_file)

#filter for user and etf
selection = df[(df['u_id']=="XY") | (df['etf_isin'] == "IE00B4L5YC18")] 

#create empty new data frame history
column_names = ["YEAR", "MONTH", "AMOUNT"]
history = pd.DataFrame(columns = column_names)

What is the next step?