📜  删除 Pandas 中的空列

📅  最后修改于: 2022-05-13 01:54:41.490000             🧑  作者: Mango

删除 Pandas 中的空列

在本文中,我们将尝试了解删除空列、空列和零值列的不同方法。首先,我们将创建一个示例数据框,然后我们将在后续示例中执行我们的操作,最后您将获得有关如何使用 Pandas 处理这种情况的强大知识。

方法:

  • 导入所需的Python库。
  • 创建示例数据框。
  • 使用 Pandas dropna() 方法,它允许用户以不同的方式分析和删除具有 Null 值的行/列。
  • 显示更新的数据框。

样本数据:

这是我们将在其上执行不同操作的示例数据框。

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)

输出: