删除 Pandas 中的空列
在本文中,我们将尝试了解删除空列、空列和零值列的不同方法。首先,我们将创建一个示例数据框,然后我们将在后续示例中执行我们的操作,最后您将获得有关如何使用 Pandas 处理这种情况的强大知识。
方法:
- 导入所需的Python库。
- 创建示例数据框。
- 使用 Pandas dropna() 方法,它允许用户以不同的方式分析和删除具有 Null 值的行/列。
- 显示更新的数据框。
Syntax: DataFrameName.dropna(axis=0, how=’any’, inplace=False)
Parameters:
- axis: axis takes int or string value for rows/columns. Input can be 0 or 1 for Integer and ‘index’ or ‘columns’ for String.
- how: how takes string value of two kinds only (‘any’ or ‘all’). ‘any’ drops the row/column if ANY value is Null and ‘all’ drops only if ALL values are null.
- inplace: It is a boolean which makes the changes in the data frame itself if True.
样本数据:
这是我们将在其上执行不同操作的示例数据框。
Python3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
# show the dataframe
print(Mydataframe)
Python3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
Python3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
nan_value = float("NaN")
Mydataframe.replace("", nan_value, inplace=True)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
Python3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
nan_value = float("NaN")
Mydataframe.replace(0, nan_value, inplace=True)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
Python3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
nan_value = float("NaN")
Mydataframe.replace(0, nan_value, inplace=True)
Mydataframe.replace("", nan_value, inplace=True)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
输出:
示例 1:
删除所有空值列。
蟒蛇3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
输出:
示例 2:
用 null 替换所有空的地方,然后用 dropna函数删除所有空值列。
蟒蛇3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
nan_value = float("NaN")
Mydataframe.replace("", nan_value, inplace=True)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
输出:
示例 3:
用 null 替换所有零位置,然后用 dropna函数删除所有空值列。
蟒蛇3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
nan_value = float("NaN")
Mydataframe.replace(0, nan_value, inplace=True)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
输出:
示例 4:
用 null 替换所有零和空位,然后用 dropna函数删除所有空值列。
蟒蛇3
# import required libraries
import numpy as np
import pandas as pd
# create a Dataframe
Mydataframe = pd.DataFrame({'FirstName': ['Vipul', 'Ashish', 'Milan'],
"Gender": ["", "", ""],
"Age": [0, 0, 0]})
Mydataframe['Department'] = np.nan
display(Mydataframe)
nan_value = float("NaN")
Mydataframe.replace(0, nan_value, inplace=True)
Mydataframe.replace("", nan_value, inplace=True)
Mydataframe.dropna(how='all', axis=1, inplace=True)
# show the dataframe
display(Mydataframe)
输出: