码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • pandas常用操作


    DataFrame

    增

    创建
    可以利用

    • 列表(list)
    • 字典(dict)
    • 系列(series)
    • Numpy ndarrays
    • 其他数据帧(DataFrame)

    来创建DataFrame对象

    如:

    a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
    
    • 1

    在这里插入图片描述

    增加列

    a['new'] = ['鹅', '鹅', '鹅']
    
    • 1

    在这里插入图片描述

    增加行

    a.loc[3]=[0, 0, 0, '鹅']
    
    • 1

    在这里插入图片描述

    删

    目标DataFrame

    b = a.copy()
    
    • 1

    在这里插入图片描述

    万能删除function

    df.drop()
    
    • 1

    添加 inplace=True 则在原数据修改,默认为False

    删列

    ①

    b.drop(columns='new')
    
    • 1

    在这里插入图片描述

    ②

    c = b.pop('new')
    b
    c
    
    • 1
    • 2
    • 3

    赋值并在原数据中删除

    在这里插入图片描述
    在这里插入图片描述
    ③

    del b['new']
    
    • 1

    在这里插入图片描述

    删行

    b.drop(index=3)
    
    • 1

    在这里插入图片描述

    改查

    这里改查放在一起,因为查到即可更改

    目标DataFrame

    a = np.random.normal(0, 1, (3, 3))
    a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
    a['abc'] = ['a', 'b', 'c']
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    loc

    pandas loc

    Access a group of rows and columns by label(s) or a boolean array.

    a = np.random.normal(0, 1, (3, 3))
    a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
    a['abc'] = ['a', 'b', 'c']
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    a.loc[0, 0]
    
    • 1

    0.41059850193837233

    a.loc[0, 'abc']
    
    • 1

    ‘a’

    a.loc[a.abc != 'b', 'abc']
    
    • 1

    在这里插入图片描述

    iloc

    pandas iloc

    Purely integer-location based indexing for selection by position.

    a.iloc[0, 3]
    
    • 1

    在这里插入图片描述

    a.iloc[0]
    
    • 1

    在这里插入图片描述

    a.iloc[[0, 1]]
    
    • 1

    在这里插入图片描述

    a.iloc[0:3]
    
    • 1

    在这里插入图片描述

    a.iloc[:, 3]
    
    • 1

    在这里插入图片描述

    [ ]

    使用[ ]进行查找

    a = np.random.normal(0, 1, (3, 3))
    a = pd.DataFrame(np.random.normal(0, 1, (3, 3)))
    a['abc'] = ['a', 'b', 'c']
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    查单列

    a[1]
    
    • 1

    在这里插入图片描述

    查多列

    a[[1, 'abc']]
    
    • 1

    在这里插入图片描述

    按单个条件查询

    a[a[1] > 0.1]
    
    • 1

    在这里插入图片描述

    a[a.index != 1]
    
    • 1

    在这里插入图片描述

    按多个条件查询

    # Satisfying multiple conditions simultaneously
    a[(a[1] > 0.1) & (a[0] > 0)]
    
    • 1
    • 2

    在这里插入图片描述

    # At least one condition is met
    a[(a[1] > 0.1) | (a[0] > 0)]
    
    • 1
    • 2

    在这里插入图片描述

    索引和字段操作

    重置

    reset_index

    Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more levels.

    a
    
    • 1

    在这里插入图片描述

    a.index = ['c', 'd', 'e']
    
    • 1

    在这里插入图片描述

    a.reset_index(drop=True, inplace=True)
    
    • 1

    在这里插入图片描述

    重命名

    rename

    Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don’t throw an error.

    a.rename(index={0:9, 1:8}, inplace=True)
    
    • 1

    在这里插入图片描述

    a.rename(columns={0:'cba', 1:'nba'}, inplace=True)
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    STL容器——vector
    Spring 事务源码分析
    【花雕动手做】有趣好玩的音乐可视化系列项目(31)--LCD1602液晶屏
    Hbase和Clickhouse对比简单总结
    为脚本创建软连接到指定的PATH中,变为自定义命令程序
    Linux /etc/passwd和/etc/shadow
    Vue 3 + TypeScript + Vite + Element-Plus + Router + Axios + Pinia项目搭建(内含完整架构)
    2023年中国分布式光纤传感产量、需求量及行业市场规模分析[图]
    RT-Thread 中断管理(学习)
    使用net/http/pprof时,发现6000端口是Chrome限制的非安全端口,报错ERR_UNSAFE_PORT
  • 原文地址:https://blog.csdn.net/sinat_28916141/article/details/127492798
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号