The modified way is the below:
df2.cache() # Marks the df2 as cachable df2.count() # Action to trigger computation and caching df2.show()