这篇文章主要介绍了python Pandas之DataFrame索引及选取数据,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
1.索引是什么
1.1 认识索引
先创建一个简单的DataFrame。文章源自设计学徒自学网-http://www.sx1c.com/25842.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
myList = [['a', 10, 1.1], ['b', 20, 2.2], ['c', 30, 3.3], ['d', 40, 4.4]]df1 = pd.DataFrame(data = myList)print(df1)--------------------------------[out]: 0 1 20 a 10 1.11 b 20 2.22 c 30 3.33 d 40 4.4 |
DataFrame中有两种索引:文章源自设计学徒自学网-http://www.sx1c.com/25842.html
- 行索引(index):对应最左边那一竖列
- 列索引(columns):对应最上面那一横行
两种索引默认均为从0开始的自增整数。文章源自设计学徒自学网-http://www.sx1c.com/25842.html
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# 输出行索引print(df1.index)[out]:RangeIndex(start=0, stop=4, step=1)---------------------------------------# 输出列索引print(df1.columns)[out]:RangeIndex(start=0, stop=3, step=1)---------------------------------------# 输出所有的值print(df1.values)[out]:array([['a', 10, 1.1], ['b', 20, 2.2], ['c', 30, 3.3], ['d', 40, 4.4]], dtype=object) |
文章源自设计学徒自学网-http://www.sx1c.com/25842.html
1.2 自定义索引
可以使用 index 这个参数指定行索引,columns 这个参数指定列索引。文章源自设计学徒自学网-http://www.sx1c.com/25842.html
|
1
2
3
4
5
6
7
8
9
10
11
|
df2 = pd.DataFrame(myList, index = ['one', 'two', 'three', 'four'], columns = ['char', 'int', 'float'])print(df2)-----------------------------------------------------------[out]: char int floatone a 10 1.1two b 20 2.2three c 30 3.3four d 40 4.4 |
输出此时的行索引和列索引:文章源自设计学徒自学网-http://www.sx1c.com/25842.html
# 输出行索引
print(df2.index)
[out]:
Index(['one', 'two', 'three', 'four'], dtype='object')
--------------------------------------------------------
# 输出列索引
print(df2.columns)
[out]:
Index(['char', 'int', 'float'], dtype='object')文章源自设计学徒自学网-http://www.sx1c.com/25842.html
文章源自设计学徒自学网-http://www.sx1c.com/25842.html
2. 索引的简单使用
文章源自设计学徒自学网-http://www.sx1c.com/25842.html
2.1 列索引
选择一列:文章源自设计学徒自学网-http://www.sx1c.com/25842.html
|
1
2
3
4
5
6
7
8
9
|
print(df2['char'])print(df2.char)# 两种方式输出一样[out]:one atwo bthree cfour dName: char, dtype: object |
注意此时方括号里面只传入一个字符串’char’,这样选出来的一列,结果的类型为Series
|
1
2
3
4
5
6
7
8
9
|
print(df2['char'])print(df2.char)# 两种方式输出一样[out]:one atwo bthree cfour dName: char, dtype: object |
选择多列:
|
1
2
3
4
5
6
7
|
print(df2[['char', 'int']])[out]: char intone a 10two b 20three c 30four d 40 |
注意此时方括号里面传入一个列表 [‘char’, ‘int’],选出的结果类型为 DataFrame。
如果只想选出来一列,却想返回 DataFrame 类型怎么办?
|
1
2
3
4
5
6
7
8
9
10
|
print(df2[['char']])[out]: charone atwo bthree cfour d---------------------------------------type(df2[['char']])[out]:pandas.core.frame.DataFrame |
注意直接使用df2[0]取某一列会报错,除非columns是由下标索引组成的,比如df1那个样子,df1[0]就不会报错。
|
1
2
3
4
5
6
7
8
9
10
11
|
print(df1[0])[out]:0 a1 b2 c3 dName: 0, dtype: object-----------------------print(df2[0])[out]: KeyError: 0 |
2.2 行索引
2.2.1 使用[ ]
区别于选取列,此种方式[ ]中不再单独的传入一个字符串,而是需要使用冒号切片。
选取行标签从 ’two’ 到 ’three’ 的多行数据
|
1
2
3
4
5
|
print(df2['two': 'three'])[out]: char int floattwo b 20 2.2three c 30 3.3 |
选取行标签为’two’这一行数据
|
1
2
3
4
5
|
# 此时返回的类型为DataFrameprint(df2['two': 'two'])[out]: char int floattwo b 20 2.2 |
在[ ]中不仅可以传入行标签,还可以传入行的编号。
选取从第1行到第3行的数据(编号从0开始)
|
1
2
3
4
5
6
|
print(df2[1:4])[out]: char int floattwo b 20 2.2three c 30 3.3four d 40 4.4 |
可以看到选取的数据是不包含方括号最右侧的编号所对应的数据的。
选取第1行的数据
|
1
2
3
4
|
print(df2[1:2])[out]: char int floattwo b 20 2.2 |
2.2.2 使用.loc()和.iloc()
区别就是.loc()是根据行索引和列索引的值来选取数据,而.iloc()是根据从0开始的下标位置来进行索引的。
选取行:
使用.loc()
|
1
2
3
4
5
6
7
8
9
10
11
12
|
print(df2.loc['one'])[out]:char aint 10float 1.1Name: one, dtype: object-------------------------------------------print(df2.loc[['one', 'three']])[out]: char int floatone a 10 1.1three c 30 3.3 |
使用.iloc()
|
1
2
3
4
5
6
7
8
9
10
11
12
|
print(df2.iloc[0])[out]:char aint 10float 1.1Name: one, dtype: object-------------------------------------------print(df2.iloc[[0, 2]])[out]: char int floatone a 10 1.1three c 30 3.3 |
到此这篇关于python Pandas之DataFrame索引及选取数据的文章就介绍到这了

评论