RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
怎么在pandas中使用DataFrame索引行列-创新互联

这期内容当中小编将会给大家带来有关怎么在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索引行列了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:怎么在pandas中使用DataFrame索引行列-创新互联
文章网址:http://sczitong.cn/article/cojjij.html