pandas dataframe删除空行或者空列dropna,一般删除指定行或者列drop

dropna参见https://blog.csdn.net/roamer314/article/details/84816171

df[~(df['col'].isnull())] #删掉空行

df.dropna(axis=0) #删除有空值的行,使用参数axis=0

df.dropna(axis=1) #删除有空值的列,使用参数axis=1

drop解释

Ipython:dataframe.drop?
Signature:
dataframe.drop(
    labels=None,
    axis=0,
    index=None,
    columns=None,
    level=None,
    inplace=False,
    errors='raise',
)
Docstring:
Drop specified labels from rows or columns.
Remove rows or columns by specifying label names and corresponding
axis, or by specifying directly index or column names. When using a
multi-index, labels on different levels can be removed by specifying
the level.
Parameters
----------
labels : single label or list-like
    Index or column labels to drop.
axis : {0 or 'index', 1 or 'columns'}, default 0
    Whether to drop labels from the index (0 or 'index') or
    columns (1 or 'columns').
index, columns : single label or list-like
    Alternative to specifying axis (``labels, axis=1``
    is equivalent to ``columns=labels``).
    .. versionadded:: 0.21.0
level : int or level name, optional
    For MultiIndex, level from which the labels will be removed.
inplace : bool, default False
    If True, do operation inplace and return None.
errors : {'ignore', 'raise'}, default 'raise'
    If 'ignore', suppress error and only existing labels are
    dropped.
Returns
-------
dropped : pandas.DataFrame
Raises
------
KeyError
    If none of the labels are found in the selected axis
See Also
--------
DataFrame.loc : Label-location based indexer for selection by label.
DataFrame.dropna : Return DataFrame with labels on given axis omitted
    where (all or any) data are missing.
DataFrame.drop_duplicates : Return DataFrame with duplicate rows
    removed, optionally only considering certain columns.
Series.drop : Return Series with specified index labels removed.

Examples
--------
 df = pd.DataFrame(np.arange(12).reshape(3,4),
                   columns=['A', 'B', 'C', 'D'])
 df
   A  B   C   D
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
Drop columns
 df.drop(['B', 'C'], axis=1)
   A   D
0  0   3
1  4   7
2  8  11
 df.drop(columns=['B', 'C'])
   A   D
0  0   3
1  4   7
2  8  11
Drop a row by index
 df.drop([0, 1])
   A  B   C   D
2  8  9  10  11
Drop columns and/or rows of MultiIndex DataFrame
 midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
                              ['speed', 'weight', 'length']],
                      codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
                             [0, 1, 2, 0, 1, 2, 0, 1, 2]])
 df = pd.DataFrame(index=midx, columns=['big', 'small'],
                   data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
                         [250, 150], [1.5, 0.8], [320, 250],
                         [1, 0.8], [0.3,0.2]])
 df
                big     small
lama    speed   45.0    30.0
        weight  200.0   100.0
        length  1.5     1.0
cow     speed   30.0    20.0
        weight  250.0   150.0
        length  1.5     0.8
falcon  speed   320.0   250.0
        weight  1.0     0.8
        length  0.3     0.2
 df.drop(index='cow', columns='small')
                big
lama    speed   45.0
        weight  200.0
        length  1.5
falcon  speed   320.0
        weight  1.0
        length  0.3
 df.drop(index='length', level=1)
                big     small
lama    speed   45.0    30.0
        weight  200.0   100.0
cow     speed   30.0    20.0
        weight  250.0   150.0
falcon  speed   320.0   250.0
        weight  1.0     0.8

 

使用df.drop删除df第一行,先取得第一行的index值:

CU2104.index.values[0]

然后传入到drop函数中,如下图所示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值