Bạn có thể lọc ra các hàng có giá trị NAN từ chuỗi cột pandas DataFrame, float, datetime e. t. c bằng cách sử dụng phương pháp
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
5 và
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
6. Python không hỗ trợ Null do đó mọi dữ liệu bị thiếu được biểu thị là Không có hoặc NaN. NaN là viết tắt của
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
7 và là một trong những cách phổ biến để biểu thị giá trị còn thiếu trong dữ liệuTrong bài viết này, tôi sẽ giải thích cách lọc ra các hàng có giá trị NAN từ cột DataFrame của gấu trúc với một số ví dụ
1. Ví dụ nhanh Lọc ra hàng NAN từ DataSelection của cột
Nếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách bỏ qua các hàng có NAN từ pandas DataFrame
# Below are some Quick examples.
# Using DataFrame.dropna[] method drop all rows that have NAN/none.
df2=df.dropna[]
# Filter out NAN data selection column by DataFrame.dropna[].
df2 = df.dropna[thresh=2]
# Pandas find columns with nan to update.
df2=df[df.Duration.notnull[]]
# Drop rows that has all NaN values.
df2=df.dropna[how='all']
# Using reset_index[] Method.
df2=df.dropna[].reset_index[drop=True]
# Two columns by using subset parameter.
df2=df.dropna[subset=['Courses','Fee']]
# Filter NAN Data selection column of strings by not operator.
df2 = df[~pd.isnull[df['Courses']]]
Bây giờ, hãy tạo một Khung dữ liệu Pandas với một vài hàng và cột và thực hiện một số ví dụ để tìm hiểu cách loại bỏ các hàng có giá trị NAN. Khung dữ liệu của chúng tôi chứa các tên cột
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
8,
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
9,
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
0 và
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
1
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
Sản lượng dưới sản lượng
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
2. Sử dụng Khung dữ liệu. Dropna[] Lọc các hàng có giá trị NAN
Bằng cách sử dụng phương pháp
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
2 bạn có thể lọc các hàng có giá trị Nan [Không phải là số] và Không có giá trị nào từ DataFrame. Lưu ý rằng theo mặc định, nó trả về bản sao của DataFrame sau khi xóa hàng. Nếu muốn xóa khỏi DataFrame hiện tại, bạn nên sử dụng
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
3
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
2Ngoài ra, bạn cũng có thể sử dụng
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
4 làm thông số để xóa các hàng có NaN, ví dụ như
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
53. Lọc ra các hàng NAN bằng DataFrame. dropna[]
Lọc ra các hàng NAN [Lựa chọn dữ liệu] bằng cách sử dụng phương pháp
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
5. Hàm
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
7 cũng có thể loại bỏ các hàng có giá trị NaN
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
8. Hàm này sẽ loại bỏ tất cả các hàng có ít nhất hai hàng không phải NaN
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
8Sản lượng dưới sản lượng
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
93. Sử dụng Khung dữ liệu. Phương thức notnull[]
Phương thức
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
6 được sử dụng để phát hiện các giá trị không bị thiếu cho một đối tượng giống như mảng. Phương thức này lấy một đối tượng vô hướng hoặc giống như mảng và cho biết liệu các giá trị có hợp lệ hay không
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
1Sản lượng dưới sản lượng
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
24. Lọc các Hàng bằng NAN bằng cách sử dụng Phương thức reset_index[]
Phương pháp
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
20 được sử dụng để tạo DataFrame hoặc Sê-ri mới có đặt lại chỉ mục. Điều này hữu ích khi chỉ mục cần được coi là một cột hoặc nó có thể được đặt lại về mặc định trước một thao tác khác
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
45. Lọc các hàng có giá trị NaN trên các cột đã chọn từ danh sách
Trong phần này, hãy xem cách chỉ loại bỏ các hàng khi các cột được chọn có giá trị NaN/Không có giá trị nào trong DataFrame, bạn có thể đạt được điều này bằng cách sử dụng tham số
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
21. Tham số tập hợp con chỉ đơn giản là chọn các hàng và cột dữ liệu cụ thể từ DataFrame [hoặc Sê-ri]
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
6Sản lượng dưới sản lượng
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
0Ngoài ra, hãy sử dụng
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
22 để xóa các hàng có tất cả các giá trị NaN/Không có giá trị nào trong một hàng [thiếu dữ liệu cho tất cả các thành phần trong một hàng]
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
1Sản lượng dưới sản lượng
Courses Fee Duration
0 Spark 22000.0 30days
1 PySpark 25000.0 NaN
2 Spark NaN 30days
3 Python NaN N/A
4 PySpark NaN NaN
5 Java NaN NaN
6. Lọc cột lựa chọn dữ liệu NAN của chuỗi theo toán tử không
Lọc dữ liệu NAN Cột chọn chuỗi bằng toán tử not[~] được sử dụng để phủ định câu lệnh
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
3Sản lượng đầu ra giống như trên
7. Hoàn thành các ví dụ để lọc các hàng có giá trị NAN
Dưới đây là một ví dụ hoàn chỉnh để lọc ra các hàng có giá trị NAN từ DataFrame
# Create a pandas DataFrame.
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Spark","Python","PySpark","Java"],
'Fee' :[22000,25000,np.nan,np.nan,np.nan,np.nan],
'Duration':['30days',np.nan,'30days','N/A', np.nan,np.nan]
}
df = pd.DataFrame[technologies]
print[df]
4Sự kết luận
Trong bài viết này, Bạn đã học cách lọc các hàng nan từ DataFrame của gấu trúc bằng cách sử dụng các phương pháp ________ 05, ________ 06. Cũng đã học cách chỉ lọc các hàng khi tất cả các giá trị là NaN/Không, chỉ khi các cột được chọn có giá trị NaN và sử dụng tham số tại chỗ