1、DataFrame 索引

1.1 普通索引取值

pandas 取行或者列的注意点:

  • 方括号写数组,表示取行,对行进行操作
  • 方括号写字符串,表示取列,对列进行操作
import pandas as pd
import numpy as np
# pandas 取行或者列的注意点
# 方括号写数组,表示取行,对行进行操作
# 方括号写字符串,表示取列,对列进行操作
t1 = pd.DataFrame(np.arange(12).reshape(3,4), index=list("abc"), columns=list("wxyz"))
print(t1)
"""w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
"""
print(t1[:2])
"""w  x  y  z
a  0  1  2  3
b  4  5  6  7
"""
print(t1[:2]["x"])
"""
a    1
b    5
Name: x, dtype: int32
"""
print(t1["y"])
"""
a     2
b     6
c    10
Name: y, dtype: int32
"""

1.2 DataFrame.loc 通过标签索引行数据

取行

t3 = pd.DataFrame(np.arange(12).reshape(3,4), index=list("abc"), columns=list("wxyz"))
print(t3)
"""w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
"""
print(t3.loc["a", "z"])  # 3   a 行 z 列
print(type(t3.loc["a", "z"]))  # <class 'numpy.int32'>
# 取第 b 行
print(t3[1:2])
print(t3.loc["a"])
print(t3.loc["a", :])
"""w  x  y  z
b  4  5  6  7
w    0
x    1
y    2
z    3
Name: a, dtype: int32
w    0
x    1
y    2
z    3
Name: a, dtype: int32
"""

取列

# 取第 y 列
print(t3["y"])
print(t3.loc[:,"y"])
"""
a     2
b     6
c    10
Name: y, dtype: int32
a     2
b     6
c    10
Name: y, dtype: int32
"""

取 多行 多列

print(t3.loc[["a","b"], ["w", "z"]])
"""w  z
a  0  3
b  4  7
"""
print(t3.loc["a":"c", ["w", "z"]])  # 注意 c 行被选中了
"""w   z
a  0   3
b  4   7
c  8  11
"""
print(t3.loc[["a","b"]])
"""w  x  y  z
a  0  1  2  3
b  4  5  6  7
"""
print(t3.loc[:, ["w", "z"]])
"""w   z
a  0   3
b  4   7
c  8  11
"""

1.3 DataFrame.iloc 通过位置获取行数据

取行

print(t3)
"""w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
"""
print(t3.iloc[1])  # 取行
"""
w    4
x    5
y    6
z    7
Name: b, dtype: int32
"""

取列

print(t3.iloc[:, 1])
"""
a    1
b    5
c    9
Name: x, dtype: int32
"""# 取多列
print(t3.iloc[:, [2,1]])
"""y  x
a   2  1
b   6  5
c  10  9
"""

取多行 多列

print(t3.iloc[[0,2], [2,1]])
"""y  x
a   2  1
c  10  9
"""
print(t3.iloc[1:,:2])
"""w  x
b  4  5
c  8  9
"""
t3.iloc[1:,:2] = 30
print(t3)
"""w   x   y   z
a   0   1   2   3
b  30  30   6   7
c  30  30  10  11
"""

2、DataFrame bool索引

print(t3)
"""w   x   y   z
a   0   1   2   3
b  30  30   6   7
c  30  30  10  11
"""
print(t3[t3["y"] > 3])
"""w   x   y   z
b  30  30   6   7
c  30  30  10  11
"""
print(t3[(t3["y"] > 3) & (t3["y"]<20)])
"""w   x   y   z
b  30  30   6   7
c  30  30  10  11
"""
print(t3[(t3["y"] > 3) |(t3["y"]<20)])
"""w   x   y   z
a   0   1   2   3
b  30  30   6   7
c  30  30  10  11
"""

3、pandas 字符串方法

data = [['Google',10],['Runoob',12],['Wiki',13]]
df = pd.DataFrame(data,columns=['Site','Age'])
print(df)
"""Site  Age
0  Google   10
1  Runoob   12
2    Wiki   13
"""
print(df[df["Site"].str.len()>4])
"""Site  Age
0  Google   10
1  Runoob   12
"""
print(df["Site"].str.split("o"))
"""
0    [G, , gle]
1    [Run, , b]
2        [Wiki]
Name: Site, dtype: object
"""
print(df["Site"].str.split("o").tolist())
"""
[['G', '', 'gle'], ['Run', '', 'b'], ['Wiki']]
"""

https://www.bilibili.com/video/BV1hx411d7jb?p=27
https://www.bilibili.com/video/BV1hx411d7jb?p=28
https://www.runoob.com/pandas/pandas-dataframe.html

python3-pandas DataFrame 索引、bool索引、pandas 字符串方法相关推荐

  1. python pandas dataframe 不显示索引_Python DataFrame 设置输出不显示index(索引)值的方法...

    Python DataFrame 设置输出不显示index(索引)值的方法 在输出代码行中,加入"index=False"如下: m_pred_survived.to_csv(&q ...

  2. pandas DataFrame(2)-行列索引及值的获取

    pandas DataFrame是二维的,所以,它既有列索引,又有行索引 上一篇里只介绍了列索引: import pandas as pddf = pd.DataFrame({'A': [0, 1, ...

  3. python pandas dataframe 不显示索引_python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)...

    前言 最近在网上搜了许多关于pandas.DataFrame的操作说明,都是一些基础的操作,但是这些操作组合起来还是比较费时间去正确操作DataFrame,花了我挺长时间去调整BUG的.我在这里做一些 ...

  4. [转载] 使用Python在Pandas Dataframe中建立索引

    参考链接: 用Pandas建立索引并选择数据 In Numpy arrays, we are familiar with the concepts of indexing, slicing, and ...

  5. python报表自动化系列 - 为pandas.DataFrame制作自然数索引(更改索引为从1开始的自然数)

    更改DataFrame索引为从1开始的自然数 [函数说明] 实际上对于人来说更习惯于从零开始索引单元格,这样也不会容易出错.而建立一个DataFrame变量后默认索引是从零开始的,还有一些由于如导入等 ...

  6. python dataframe去掉索引_python中pandas.DataFrame(创建、索引、增添与删除)的简单操作方法介绍...

    这篇文章python中pandas.DataFrame(创建.索引.增添与删除)的简单操作方法介绍,其中包括创建.索引.增添与删除等的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看 ...

  7. pandas.DataFrame的pivot()和unstack()实现行转列

    示例: 有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from warnings impor ...

  8. 向pandas DataFrame添加一行

    本文翻译自:Add one row to pandas DataFrame I understand that pandas is designed to load fully populated D ...

  9. python dataframe增加一行_python - 在pandas.DataFrame中添加一行

    python - 在pandas.DataFrame中添加一行 据我所知,pandas旨在加载完全填充的DataFrame,但我需要创建一个空的DataFrame,然后逐个添加行.做这个的最好方式是什 ...

  10. 如何迭代pandas dataframe的行

    from:https://blog.csdn.net/tanzuozhev/article/details/76713387 How to iterate over rows in a DataFra ...

最新文章

  1. Java 接口interface
  2. zcmu 4935(排序)
  3. java 文件下载,中文表名,中文内容
  4. 利用Python来玩扫雷,极致的思维体验
  5. winform程序的皮肤问题
  6. apache的站点安全1
  7. c语言答案纪纲,重庆理工大学C语言程序设计基础教程习题答案(纪纲金艳).doc
  8. 破解tinypng文件上传的限制
  9. 8位PCM编码转换16位PCM
  10. 计算机房铺设防尘,机房装修施工方案机房防尘漆施工工艺.doc
  11. 【Java从零到架构师第③季】【项目实战】驾考管理系统
  12. Android开启指纹验证
  13. HTTP和MQTT协议实践
  14. IOS双重认证增加手机号
  15. 【软件测试】可以写进简历的项目实战内容
  16. 移动机器人轮式里程计
  17. UniAPP HBuilderX 运行到各个小程序开发工具
  18. 【初等数论】个人数论总结
  19. 在windows上搭建DZ(Discuz)论坛-部署完成
  20. 分数阶导数的意义_导数的意义

热门文章

  1. ssh免密登陆机制示意图
  2. Ubuntu14.04 Apollo 3.5安装
  3. Databricks 企业版 SparkDelta Lake 引擎助力 Lakehouse 高效访问
  4. 蚂蚁金服开放计算架构:下一代金融级计算架构
  5. 左手代码右手滑板 支付宝这个程序员有些酷
  6. 为什么选择Cassandra
  7. 来自 Spring Cloud 官方的消息,Spring Cloud Alibaba 即将毕业
  8. 为什么说优秀架构师往往是一个悲观主义者?
  9. 深入解读MySQL8.0 新特性 :Crash Safe DDL 1
  10. I+关系网络分析发布,提供完整的可视化分析和关系引擎功能