温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在pandas 中使用DataFrame索引行列

发布时间:2021-06-01 18:13:02 来源:亿速云 阅读:235 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关怎么在pandas 中使用DataFrame索引行列,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

  • 行索引

索引行有三种方法,分别是 loc iloc ix

import pandas as pd import numpy as np index = ["a", "b", "c", "d"] data = np.random.randint(10, size=(4, 3)) df = pd.DataFrame(data, index=index) """   0 1 2 a 9 7 1 b 0 0 7 c 2 6 5 d 8 2 5 """

loc

loc通过行索引名字来确定行的

单行索引, 返回Series对象

df.loc["a"] """ 0  9 1  7 2  1 Name: a, dtype: int64 """ df.loc["b"] """ 0  0 1  0 2  7 Name: b, dtype: int64 """

多行索引, 返回DataFrame对象

df.loc[["a", "c"]] """   0 1 2 a 9 7 1 c 2 6 5 """

iloc

通过行索引序号来确定行的

单行索引, 返回Series对象

df.iloc[0] """ 0  9 1  7 2  1 Name: a, dtype: int64 """ df.iloc[1] """ 0  0 1  0 2  7 Name: b, dtype: int64 """

多行索引, 返回DataFrame对象

df.iloc[[0, 2]] """   0 1 2 a 9 7 1 c 2 6 5 """

ix(不建议使用)

通过行索引名字或序号来确定行的, 如果行索引 index 的类型为整型时, 使用 ix 方法索引时为按行索引名字进行索引, 如行索引名不存在则会报错

index = [2, 3, 4, 5] df = pd.DataFrame(data, index=index) """   0 1 2 2 9 7 1 3 0 0 7 4 2 6 5 5 8 2 5 """ df.ix[2] """ 0  9 1  7 2  1 Name: 2, dtype: int64 """ # 提示信息 """ .ix is deprecated. Please use .loc for label based indexing or .iloc for positional indexing """ # 如果 index 为整数, 则不能按行索引号进行索引 df.ix[0] """ ... KeyError: 0 """

列索引

索引行有两种方法,分别是 . []

import pandas as pd import numpy as np columns = ["i", "ii", "iii"] data = np.random.randint(10, size=(4, 3)) df = pd.DataFrame(data, columns=columns) """   i ii iii  0 4  5  9  1 0  3  4  2 7  9  1  3 8  2  3  """

通过 . 属性直接获取指定行, 返回Series对象

df.i """ 0  4 1  0 2  7 3  8 Name: i, dtype: int64 """

 []

单列索引, 返回DataFrame对象

df[["i"]] """   i 0 4 1 0 2 7 3 8 """

多列索引, 返回DataFrame对象

df[["i", "ii"]] """   i ii 0 4  5 1 0  3 2 7  9 3 8  2 """

同时索引行及列

通过指定索引名或切片方式进行索引

index = ["a", "f", "c", "h"] columns = ["i", "ii", "iii"] df = pd.DataFrame(data, index=index, columns=columns) """   i ii iii a 4  5  9 f 0  3  4 c 7  9  1 h 8  2  3 """

loc

通过指定行及列索引名进行索引, 返回DataFrame对象

df.loc[["a", "f"], ["ii", "iii"]] """   ii iii a  5  9 f  3  4 """

通过指定行及列索引名范围进行索引(包含边值), 返回DataFrame对象

df.loc["a":"c", "ii":"iii"] """   ii iii a  5  9 f  3  4 c  9  1 """

iloc

通过指定行及列索引号进行索引, 返回DataFrame对象

df.iloc[[0, 1], [1, 2]] """   ii iii a  5  9 f  3  4 """

通过指定行及列索引号范围进行切片索引(左闭右开), 返回DataFrame对象

df.iloc[:3, 1:3] """   ii iii a  5  9 f  3  4 c  9  1 """

ix(不建议使用)

通过指定行及列索引号范围或名字范围进行切片, 返回DataFrame对象

df.ix["a":"c", "i":"iii"] df.ix["a":"c", 1:3] df.ix[:3, 1:3]

上述就是小编为大家分享的怎么在pandas 中使用DataFrame索引行列了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI