Để loại bỏ giá trị NaN khỏi DataFrame không dùng fillna hay interpolate, thực hiện các bước sau:
Các bước
- Thiết lập kích thước hình và padding.
- Tạo mảng numpy chứa NaN.
- Tạo Pandas Series từ mảng.
- Vẽ nội suy (interpolate) với method 'index' dùng giá trị số thực của index.
- Hiển thị hình bằng show().
Ví dụ
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.array([1., 1.2, 0.89, np.NAN,
1.2, np.NAN, 1.89, 2.09,
.78, .67, np.NAN, 1.78,
np.NAN, 1.56, 1.89, 2.78])
df = pd.Series(data)
df.interpolate('index').plot(marker='o')
plt.show()
Kết quả
Đồ thị đường nối các điểm sau khi nội suy tuyến tính theo index, các NaN được điền giá trị ước lượng.