JeeyCi
JeeyCi

Reputation: 597

python - how to Interpolate NaN values

Is there a way to interpolate Nan values of P & C in final_df (where str is a range with equal step) ??

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df0 = pd.DataFrame({'str': [var for var in range(700,1260,5)]})
print(df0)

df1 = pd.DataFrame({'str': [700,705,710,715,720,1095,1100,1105,1110,1115,1120,1125,1130,1135,1205,1210,1215,1220,1225,1230,1235,1240,1245,1250,1255],
 'P': [0.075,0.075,0.075,0.075,0.075,17.95,19.75,21.85,24.25,26.55,29.2,31.9,35.05,37.7,98.6,102.15,108.5,113.5,118.4,123.55,127.3,132.7,138.7,142.7,148.35],
 'C': [407.8,403.65,398.3,391.65,387.8,30.05,26.65,23.7,21.35,19.65,16.05,14.3,11.95,9.95,0.475,0,0.525,0,0.2,0.175,0.15,0.375,0.125,0.075,0.175]})

df = pd.merge(df0,df1, on="str", how="outer")
print(df)

df.interpolate(method ='cubic', limit_direction ='both') seems not working...

Upvotes: 0

Views: 470

Answers (1)

Immature trader
Immature trader

Reputation: 159

I find inplace=True missing from your code. Insert this in your interpolate to replace the Nan values.

df.interpolate(method='cubic', limit_direction='both', inplace=True)

Upvotes: 1

Related Questions