r/dataanalysis • • Aug 05 '23

How can I fix this?

Post image

I have created a dataframe called semi_df in previous cells. but when I close and open the same python file in my jupyter notebook the cells below cannot read the data frames in the cells above them i have to go to the cells and hit shift +enter again and again so that the cells below can read the code above them...

0 Upvotes

11 comments sorted by

5

u/CaptainFoyle Aug 05 '23

Did you run the previous cells? Otherwise, it's probably a spelling mistake.

-4

u/vich_lasagna Aug 05 '23

The thing is every time I open the file I have to run each and every previous cell, which is kinda annoying. I wanted to know if there is a permanent fix to this

28

u/CaptainFoyle Aug 05 '23

That's how Jupyter notebooks work, you have to run the previous cells if you want to use variables that were declared there

6

u/vich_lasagna Aug 05 '23

I see. Thanks for your help

5

u/stitchasaurusrex Aug 05 '23

You can select any cell in your notebook and go to the Cell menu > run all above. That way you don’t have to run each preceding cell individually.

31

u/DoctorFuu Aug 05 '23

Have you tried defining semi_df?

(I see a [1] which could indicate that this is the first cell you've run in this session, if semi_df was defined in another cell you need to run it first)

2

u/data-lite Aug 07 '23

This.

2

u/Anti-ThisBot-IB Aug 07 '23

Hey there data-lite! If you agree with someone else's comment, please leave an upvote instead of commenting "This."! By upvoting instead, the original comment will be pushed to the top and be more visible to others, which is even better! Thanks! :)


I am a bot! If you have any feedback, please send me a message! More info: Reddiquette

5

u/[deleted] Aug 05 '23 edited Aug 06 '23

An easy makeshift - if you don't want to run all the previous cells and redo all operations - is to simply save your dataframe and reload when you start again.

semi_df.to_csv("semi_df.csv")

Then you just pick up again loading your frame with semi_df = pd.read_csv("semi_df.csv").

This will save you from running everything again and you are right there where you left of.

Alternatively you can pickle all data too. I usually save all important data and variables in a pickle dict.