tonglin0325的个人主页

Python学习笔记——pandas

官方文档:https://pandas.pydata.org/docs/reference/index.html

1.loc属性,通过标签或布尔数组访问一组行和列。pandas.DataFrame.loc

1
2
3
4
5
6
7
8
9
>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
... index=['cobra', 'viper', 'sidewinder'],
... columns=['max_speed', 'shield'])
>>> df
max_speed shield
cobra 1 2
viper 4 5
sidewinder 7 8

获取单个label,返回是一个series

1
2
3
4
5
>>> df.loc['viper']
max_speed 4
shield 5
Name: viper, dtype: int64

获取List of labels。注意使用 [[]] 返回的是一个 DataFrame

1
2
3
4
5
>>> df.loc[['viper', 'sidewinder']]
max_speed shield
viper 4 5
sidewinder 7 8

通过行标签和列标签来获得具体值

1
2
3
>>> df.loc['cobra', 'shield']
2

对行进行切片,对列进行单标签切片(取得部分行和部分列)

1
2
3
4
5
>>> df.loc['cobra':'viper', 'max_speed']
cobra 1
viper 4
Name: max_speed, dtype: int64

通过boolean值来选取部分行

1
2
3
4
>>> df.loc[[False, False, True]]
max_speed shield
sidewinder 7 8

条件筛选符合条件的行或者列

1
2
3
4
>>> df.loc[df['shield'] > 6]
max_speed shield
sidewinder 7 8

条件筛选后再指定某个列

1
2
3
4
>>> df.loc[df['shield'] > 6, ['max_speed']]
max_speed
sidewinder 7

多个条件

1
2
3
4
5
6
7
8
9
>>> df.loc[(df['max_speed'] > 1) &amp; (df['shield'] < 8)]
max_speed shield
viper 4 5

>>> df.loc[(df['max_speed'] > 4) | (df['shield'] < 5)]
max_speed shield
cobra 1 2
sidewinder 7 8