I have dataset with the following columns:

* encounterDate
* FormName
* PersonID

In the FormName, I have the following forms:

1. Baseline
2. Follow-up

Here's a sample data:

```
encounterDate, FormName, PersonID
2019-01-12, Baseline, 01
2020-01-01, Baseline, 01
2019-04-12, Follow-up, 01
2019-13-12, Follow-up, 01
2020-15-01, Follow-up, 01

```

I would like to have the following table:

```
encounterDate, FormName, PersonID, Previous_date
2019-01-12, Baseline, 01, null
2020-01-01, Baseline, 01, 2019-01-12
2019-04-12, Follow-up, 01, null
2019-13-12, Follow-up, 01, 2019-04-12
2020-15-01, Follow-up, 01, 2019-13-12

```

How do I write this code in Python?

Additionally, I would like to also rank them:

```
encounterDate, FormName, PersonID, Previous_date, Rank
2019-01-12, Baseline, 01, null, 1
2020-01-01, Baseline, 01, 2019-01-12, 2
2019-04-12, Follow-up, 01, null, 1
2019-13-12, Follow-up, 01, 2019-04-12, 2
2020-15-01, Follow-up, 01, 2019-13-12, 3

```

Here's my working code in SQL

```
select encounter_date,FormName,PersonID
, date((select max(enc.encounter_datetime) 
                from encounter enc 
                where enc.patient_id=e.patient_id 
                        and enc.encounter_type=e.encounter_type 
                        and date(e.encounter_datetime)>date(enc.encounter_datetime))) previous_date

from encounter e

```

Thank you in advance.

John