Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ลบค่า NaN จาก dataframe โดยไม่ใช้ fillna หรือ Interpolate (Python Matplotlib)

ในการลบค่า NaN จาก dataframe โดยไม่ใช้ fillna หรือ interpolate สามารถทำตามขั้นตอนดังนี้ (ตัวอย่างใช้ interpolate เพื่อแสดงการเติมค่า):

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • สร้างอาร์เรย์เพื่อทำ Pandas data frame

  • One-dimensional ndarray พร้อม axis labels (รวม time series)

  • Plotting interpolation 'index', 'values' ใช้ค่าตัวเลขจริงของ index

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

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
# Numpy array
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]
                )
# Pandas dataframe
df = pd.Series(data)
# วาด interpolation
df.interpolate('index').plot(marker='o')
# แสดง plot
plt.show()

ผลลัพธ์

จะแสดงกราฟเส้น 16 จุด NaN ถูกเติมด้วย interpolation ตาม index

ลบค่า NaN จาก dataframe โดยไม่ใช้ fillna หรือ Interpolate (Python Matplotlib)