删除dataframe熊猫中的空列?

时间:2022-05-27 07:19:43

I have a dataFrame in pandas and several of the columns have all null values. Is there a built in function which will let me remove those columns?

我在熊猫中有一个dataFrame,其中几个列都有空值。是否有一个内置的函数可以让我移除这些列?

Thank you!

谢谢你!

2 个解决方案

#1


87  

Yes, dropna. See http://pandas.pydata.org/pandas-docs/stable/missing_data.html and the DataFrame.dropna docstring:

是的,dropna。请参见http://pandas.pydata.org/pandas-docs/stable/missing_data.html和DataFrame。dropna文档字符串:

Definition: DataFrame.dropna(self, axis=0, how='any', thresh=None, subset=None)
Docstring:
Return object with labels on given axis omitted where alternately any
or all of the data are missing

Parameters
----------
axis : {0, 1}
how : {'any', 'all'}
    any : if any NA values are present, drop that label
    all : if all values are NA, drop that label
thresh : int, default None
    int value : require that many non-NA values
subset : array-like
    Labels along other axis to consider, e.g. if you are dropping rows
    these would be a list of columns to include

Returns
-------
dropped : DataFrame

The specific command to run would be:

要运行的具体命令是:

df=df.dropna(axis=1,how='all')

#2


-1  

Function for removing all null columns from the data frame:

函数,用于从数据框中删除所有空列:

def Remove_Null_Columns(df):
    dff = pd.DataFrame()
    for cl in fbinst:
        if df[cl].isnull().sum() == len(df[cl]):
            pass
        else:
            dff[cl] = df[cl]
    return dff 

This function will remove all Null columns from the df.

这个函数将从df中删除所有空列。

#1


87  

Yes, dropna. See http://pandas.pydata.org/pandas-docs/stable/missing_data.html and the DataFrame.dropna docstring:

是的,dropna。请参见http://pandas.pydata.org/pandas-docs/stable/missing_data.html和DataFrame。dropna文档字符串:

Definition: DataFrame.dropna(self, axis=0, how='any', thresh=None, subset=None)
Docstring:
Return object with labels on given axis omitted where alternately any
or all of the data are missing

Parameters
----------
axis : {0, 1}
how : {'any', 'all'}
    any : if any NA values are present, drop that label
    all : if all values are NA, drop that label
thresh : int, default None
    int value : require that many non-NA values
subset : array-like
    Labels along other axis to consider, e.g. if you are dropping rows
    these would be a list of columns to include

Returns
-------
dropped : DataFrame

The specific command to run would be:

要运行的具体命令是:

df=df.dropna(axis=1,how='all')

#2


-1  

Function for removing all null columns from the data frame:

函数,用于从数据框中删除所有空列:

def Remove_Null_Columns(df):
    dff = pd.DataFrame()
    for cl in fbinst:
        if df[cl].isnull().sum() == len(df[cl]):
            pass
        else:
            dff[cl] = df[cl]
    return dff 

This function will remove all Null columns from the df.

这个函数将从df中删除所有空列。