pandas中iloc()函数

pandas中iloc()函数

DataFrame.iloc
纯基于整数位置的索引。

import pandas as pd
mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
           {'a': 100, 'b': 200, 'c': 300, 'd': 400},
           {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
'''mydict
[{'a': 1, 'b': 2, 'c': 3, 'd': 4},
 {'a': 100, 'b': 200, 'c': 300, 'd': 400},
 {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000}]'''
df = pd.DataFrame(mydict)
'''
df
	a	b	c	d
0	1	2	3	4
1	100	200	300	400
2	1000	2000	3000	4000

'''

pandas中iloc()函数_第1张图片

df.iloc[0]#取第0行
a    1
b    2
c    3
d    4
Name: 0, dtype: int64
df.iloc[0].shape
(4,)
type(df.iloc[0].shape)
tuple

df.iloc[[0]]
	a	b	c	d
0	1	2	3	4
type(df.iloc[[0]])
pandas.core.frame.DataFrame

df.iloc[[0,2]]#取第0、2行
	   a	   b	   c	   d
0	   1	   2	   3	   4
2	1000	2000	3000	4000

df.iloc[0:2,0:3]#取0到1行和0到2列
	  a	  b   c
0	  1	  2	  3
1	100	200	300

df.iloc[[True, False, True]]#不常用
	   a	   b	   c	   d
0	   1	   2	   3	   4
2	1000	2000	3000	4000

df.iloc[lambda x: x.index % 2 == 0]#函数生成索引列表,x即df
       a	   b	   c	   d
0	   1	   2	   3	   4
2	1000	2000	3000	4000

你可能感兴趣的:(python,机器学习,深度学习)