Để xóa cột toàn giá trị null, dùng dropna() với tham số how='all'.
Nhập thư viện:
import pandas as pd
import numpy as np
Tạo DataFrame, đặt NaN bằng np.nan (sửa từ np.NAN):
dataFrame = pd.DataFrame({
"Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],
"Result": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]
})
Xóa cột toàn null:
dataFrame.dropna(how='all', axis=1, inplace=True)
Ví dụ
import numpy as np
import pandas as pd
dataFrame = pd.DataFrame({
"Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],
"Result": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]
})
print("DataFrame ...\n", dataFrame)
dataFrame.dropna(how='all', axis=1, inplace=True)
print("\nDataFrame ...\n", dataFrame)
Đầu ra
DataFrame ... Result Student 0 NaN Jack 1 NaN Robin 2 NaN Ted 3 NaN Robin 4 NaN Scarlett 5 NaN Kat 6 NaN Ted DataFrame ... Student 0 Jack 1 Robin 2 Ted 3 Robin 4 Scarlett 5 Kat 6 Ted