📅  最后修改于: 2020-10-29 03:06:45             🧑  作者: Mango
如果您的数据集包含空值,则可以使用dropna()函数分析并删除数据集中的行/列。
DataFrameName.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
how
:当我们拥有至少一个NA或全部NA时,确定是否从DataFrame中删除行或列。它只接受两种字符串值(“ any”或“ all”)。
subset
:这是一个数组,用于限制删除过程,以限制通过列表传递的行/列。它返回删除了NA条目的DataFrame。
对于演示,首先,我们获取一个csv文件,该文件将从数据集中删除任何列。
import pandas as pd
aa = pd.read_csv("aa.csv")
aa.head()
输出量
Name | Hire Date | Salary | Leaves Remaining |
---|---|---|---|
0 John Idle 03/15/14 | 50000.0 | 10 | |
1 Smith Gilliam | 06/01/15 | 65000.0 | 8 |
2 Parker Chapman | 05/12/14 | 45000.0 | 10 |
3 Jones Palin | 11/01/13 | 70000.0 | 3 |
4 Terry Gilliam | 08/12/14 | 48000.0 | 7 |
5 Michael Palin | 05/23/13 | 66000.0 | 8 |
# importing pandas module
import pandas as pd
# making data frame from csv file
info = pd.read_csv("aa.csv")
# making a copy of old data frame
copy = pd.read_csv("aa.csv")
# creating value with all null values in new data frame
copy["Null Column"]= None
# checking if column is inserted properly
print(info.columns.values, "\n", copy.columns.values)
# comparing values before dropping null column
print("\nColumn number before dropping Null column\n",
len(info.dtypes), len(copy.dtypes))
# dropping column with all null values
copy.dropna(axis = 1, how ='all', inplace = True)
# comparing values after dropping null column
print("\nColumn number after dropping Null column\n",
len(info.dtypes), len(info.dtypes))
输出量
[' Name Hire Date Salary Leaves Remaining']
[' Name Hire Date Salary Leaves Remaining'
'Null Column']
Column number before dropping Null column
1 2
Column number after dropping Null column
1 1
上面的代码从数据集中删除了null列,并返回了一个新的DataFrame。