How to fix the problem model.fit(df[['size2']]) NameError: name 'df' is not defined

As the error suggests, it seems that you defined data when you actually wanted to define df. Therefore, changing this line from:

data = pd.read_csv('P.csv',encoding='latin-1', on_bad_lines='skip',index_col=0, header=0, 

To:

df = pd.read_csv('P.csv',encoding='latin-1', on_bad_lines='skip',index_col=0, header=0, 

Would probably solve your issue.