How to use Interpolate() In Python Pandas
It estimates and fills missing values by linearly interpolating between neighboring data points, creating a smoother dataset. It is particularly useful for time series data. Use df.interpolate( )
to perform and replace NaN values with interpolated values in-place.
Python3
import pandas as pd import numpy as np dit = pd.DataFrame({ 'August' : [ 32 , 34 , 4.85 , 71.2 , 1.1 ], 'September' : [ 54 , 68 , 9.25 , np.nan, 0.9 ], 'October' : [ 5.8 , 8.52 , np.nan, 1.6 , 11 ], 'November' : [ 5.8 , 50 , 8.9 , 77 , 78 ]}) dit |
Output:
August September October November
0 32.00 54.00 5.80 5.8
1 34.00 68.00 8.52 50.0
2 4.85 9.25 NaN 8.9
3 71.20 NaN 1.60 77.0
4 1.10 0.90 11.00 78.0
Python3
dit = dit.interpolate() dit |
Output:
August September October November
0 32.00 54.000 5.80 5.8
1 34.00 68.000 8.52 50.0
2 4.85 9.250 5.06 8.9
3 71.20 5.075 1.60 77.0
4 1.10 0.900 11.00 78.0
How to Drop Rows with NaN Values in Pandas DataFrame?
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results. In this article, we will discuss how to drop rows with NaN values.