码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • pandas drop 方法


    pandas.Series.drop

    Series.drop 方法可以返回一个新对象,移除指定的 index labels.

    import pandas as pd
    import numpy as np
    
    s = pd.Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
    s
    """
    a    0.0
    b    1.0
    c    2.0
    d    3.0
    e    4.0
    dtype: float64
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    s2 = s.drop('c')
    s2
    """
    a    0.0
    b    1.0
    d    3.0
    e    4.0
    dtype: float64
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    s.drop(['d', 'c'])
    """
    a    0.0
    b    1.0
    e    4.0
    dtype: float64
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    pandas.DataFrame.drop

    对于 DataFrame,我们可以通过指定 axis 参数来决定从哪个轴进行 drop。

    df = pd.DataFrame(np.arange(16).reshape((4, 4)),
                        index=['BeiJ', 'ShangH', 'ShenZ', 'GuangZ'],
                        columns=['one', 'two', 'three', 'four'])
    print(df)
    """
            one  two  three  four
    BeiJ      0    1      2     3
    ShangH    4    5      6     7
    ShenZ     8    9     10    11
    GuangZ   12   13     14    15
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    只传入一个序列,则丢弃的是 index:

    df.drop(['BeiJ', 'ShenZ'])
    """
            one  two  three  four
    ShangH    4    5      6     7
    GuangZ   12   13     14    15
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    df.drop(['BeiJ', 'ShenZ']) 其实是 df.drop(index=['BeiJ', 'ShenZ'])


    将 axis 参数设为 1 或者 ‘columns’,则会丢弃 column:

    df.drop(['one', 'three'], axis=1)
    """
            two  four
    BeiJ      1     3
    ShangH    5     7
    ShenZ     9    11
    GuangZ   13    15
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    以上语句和下面的表达是等价的:

    df.drop(columns=['one', 'three'])
    """
            two  four
    BeiJ      1     3
    ShangH    5     7
    ShenZ     9    11
    GuangZ   13    15
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    如果我们不想要创建新的对象,而是直接在原对象基础之上修改,则可以指定 inplace=True:

    df.drop(['BeiJ', 'ShenZ'], inplace=True)
    print(df)
    """
            one  two  three  four
    ShangH    4    5      6     7
    GuangZ   12   13     14    15
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    References

    [1] NumPy Reference. https://numpy.org/doc/stable/reference/index.html
    [2] Python for Data Analysis, 2 n d ^{\rm nd} nd edition. Wes McKinney.

  • 相关阅读:
    Mint_21.3 drawing-area和goocanvas的FB笔记(八)
    进程的内容
    GBase 8a语法格式
    TMS320F28374S之ADC一
    Python文件存读取
    四.Kafka入门到精通-SpringBoot整合Kafka(Producer拦截器&Producer监听器)
    layui--记录
    【2022】Nginx使用ngx_http_upstream_module实现负载均衡功能
    2023-2028年中国高纯纯碱市场运营态势及发展趋势预测报告
    natfrp和FRP配置SSL的基本步骤和bug排查
  • 原文地址:https://blog.csdn.net/myDarling_/article/details/128026198
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号