Nan values in columns when creating a dataframe

Category: python pandas dataframe (4 Views)

I try to create a dataframe with columns. I import values of these columns from other dataframe that are not empty. But when i create it,, i get an empty dataframe with Nan VAlues .

Here the code :

# initialize data of lists.
comparaison = {'actual': imp_all['prix_moyen'].tail(100),
        'predicted': best_model.fittedvalues.tail(100)}

print(comparaison)
  
# Creates pandas DataFrame.
df = pd.DataFrame(comparaison, index = pd.date_range(start='2022-08-25', periods=100))

print(df)

Result :

{'actual': 1149    0.001313
1150    0.001381
1151    0.001297
1152    0.001412
1153    0.001480
          ...   
1244    0.001474
1245    0.001467
1246    0.001492
1247    0.001530
1248    0.001424
Name: prix_moyen, Length: 100, dtype: float64, 'predicted': 1149    0.001371
1150    0.001340
1151    0.001342
1152    0.001306
1153    0.001377
          ...   
1244    0.001477
1245    0.001393
1246    0.001354
1247    0.001326
1248    0.001356
Length: 100, dtype: float64}
            actual  predicted
2022-08-25     NaN        NaN
2022-08-26     NaN        NaN
2022-08-27     NaN        NaN
2022-08-28     NaN        NaN
2022-08-29     NaN        NaN
...            ...        ...
2022-11-28     NaN        NaN
2022-11-29     NaN        NaN
2022-11-30     NaN        NaN
2022-12-01     NaN        NaN
2022-12-02     NaN        NaN

[100 rows x 2 columns]

Can you help me to fix this error?

thanks

🟢 Solution

The reason is that you try to assign an index to the series actual and predicted which apparently don't have datetime index. Try to alter the index after defining the new dataframe:

df = pd.DataFrame(comparaison)
df.index = pd.date_range(start='2022-08-25', periods=100)