先来看一下示例:
#!/usr/bin/python
import pandas as pd
# 三个字段 name, site, age
nme = ["Google", "Runoob", "Taobao", "Wiki"]
st = ["www.google.com", "www.runoob.com", "www.taobao.com", "www.wikipedia.org"]
ag = [90, 40, 80, 90]
# 字典
dict = {'name': nme, 'site': st, 'age': ag}
df = pd.DataFrame(dict, index=[1,2,3,4])
print(df)
print(df.loc[1:3,['name','site']])
print(df.iloc[1:3,[0,1]])
输出:
name site age
1 Google www.google.com 90
2 Runoob www.runoob.com 40
3 Taobao www.taobao.com 80
4 Wiki www.wikipedia.org 90
name site
1 Google www.google.com
2 Runoob www.runoob.com
3 Taobao www.taobao.com
name site
2 Runoob www.runoob.com
3 Taobao www.taobao.com
将df的索引设置成了整数的1,2,3,4;
df.loc[row selection, column selection]
其中loc的row是依据指定的标签来查找的,比如这里查找的就是标签1到3的行数据。
df.iloc [row selection, column selection]
其中iloc的row是根据索引来查找的,比如这里查找的就是第一行到第三行的数据,取值范围是length-1,这里即3-1行数据;其中iloc的i即是index的意思。
df = pd.DataFrame(dict, index=[1,2,3,4])
print('df数据:\n',df,'\n')
#print(df.loc[1:3,['name','site']])
print('iloc取第一行:\n',df.iloc[0],'\n')
print('iloc取最后一行:\n',df.iloc[-1],'\n')
out:
df数据:
name site age
1 Google www.google.com 90
2 Runoob www.runoob.com 40
3 Taobao www.taobao.com 80
4 Wiki www.wikipedia.org 90
iloc取第一行:
name Google
site www.google.com
age 90
Name: 1, dtype: object
iloc取最后一行:
name Wiki
site www.wikipedia.org
age 90
Name: 4, dtype: object
其中iloc只取一行时返回的是Series数据。
传递列表可转为Dataframe:
print('iloc取第一行:\n',df.iloc[[0]],'\n')
out:
iloc取第一行:
name site age
1 Google www.google.com 90
loc:
1.定义
loc按照标签或者索引、布尔值或者条件进行选择数据,这种选择数据的方法较为常用。
(1)根据索引选择单行或单列
df = pd.DataFrame(dict, index=[1,2,3,4])
print('df数据:\n',df,'\n')
df.set_index('name', inplace=True)
print('设置name为索引:\n', df, '\n')
print('loc查找:\n', df.loc[['Google','Wiki']], '\n')
out:
df数据:
name site age
1 Google www.google.com 90
2 Runoob www.runoob.com 40
3 Taobao www.taobao.com 80
4 Wiki www.wikipedia.org 90
设置name为索引:
site age
name
Google www.google.com 90
Runoob www.runoob.com 40
Taobao www.taobao.com 80
Wiki www.wikipedia.org 90
loc查找:
site age
name
Google www.google.com 90
Wiki www.wikipedia.org 90
(2)使用loc进行布尔值/逻辑索引
查找site为"www.google.com"的数据,在查找一些特定属性的数据时有用。
print('loc查找:\n', df.loc[df['site'] == 'www.google.com'], '\n')
out:
loc查找:
site age
name
Google www.google.com 90
(3)使用loc修改Dateframe中的值
先选出想要的数据,然后进行赋值操作。
df.loc[df['site'] == 'www.google.com', 'age'] = 80
out:
loc查找:
site age
name
Google www.google.com 80
df.loc[df['age'] > 80, 'site'] = 'www.google.com'
print('loc查找:\n', df.loc[df['site'] == 'www.google.com'], '\n')
out:
loc查找:
site age
name
Google www.google.com 90
Wiki www.google.com 90
#这样查找也可以,但功能比loc少很多
print(df[df['age'] > 80])
out:
name site age
1 Google www.google.com 90
4 Wiki www.wikipedia.org 90
模糊查找,使用startswith和endswith
print('查找site以www开头的数据:\n', df.loc[df['site'].str.startswith('www')], '\n')
print('查找site以.com结尾的数据:\n', df.loc[df['site'].str.endswith('.com')], '\n')
out:
查找site以www开头的数据:
name site age
1 Google www.google.com 90
2 Runoob www.runoob.com 40
3 Taobao www.taobao.com 80
4 Wiki www.wikipedia.org 90
查找site以.com结尾的数据:
name site age
1 Google www.google.com 90
2 Runoob www.runoob.com 40
3 Taobao www.taobao.com 80
参考:Pandas经典用法:数据筛选之iloc和loc_我爱Python数据挖掘的博客-CSDN博客_pandas.iloc