Python学习之:pandas 索引 loc,iloc ;数据过滤和筛选

import pandas as pd
columns = ['name','age','sex']
data = [['wang',15,'male'],
        ['li',20,'female']]
df = pd.DataFrame(data=data,columns=columns)
df
name age sex
0 wang 15 male
1 li 20 female

bool 过滤数据

df.sex =='male'
0     True
1    False
Name: sex, dtype: bool
# 使用 bool 值来决定 df 内的所有值
df[df.sex == "male"] = 1
df
name age sex
0 1 1 1
1 li 20 female
# 整体替换 index 
df.index = ['student1','student2']
df
name age sex
student1 wang 15 male
student2 li 20 female

loc 索引

# 索引使用了字符串,没有使用列表,返回的数据是 series 形式
df.loc['student1']
name    wang
age       15
sex     male
Name: student1, dtype: object
# loc 里面先行后列索引
df.loc['student1',['name','sex']]
name    wang
sex     male
Name: student1, dtype: object
# 只要索引的时候,行索引和列索引都使用列表的形式,输出的就是个 DataFrame
# 如果一个不使用列表,那么整体就是个 Series
df.loc[['student1'],['name','sex']]
name sex
student1 wang male
df.loc[['student1','student2'],['name','sex']]
name sex
student1 wang male
student2 li female

iloc 索引

# index_loc的 索引方式
df.iloc[0,[0,2]]
name    wang
sex     male
Name: student1, dtype: object
df.iloc[[0,1],[0,2]]
name sex
student1 wang male
student2 li female
columns = ['id','age','sex']
data = [[13281,15,1],
        [11111,20,0],
       [2222,18,1],
       [543232,9,0]]
df = pd.DataFrame(data=data,columns=columns)
df.index = ['student1','student2','student3','student4']
df
id age sex
student1 13281 15 1
student2 11111 20 0
student3 2222 18 1
student4 543232 9 0
# 行列都是切片索引返回也是 DataFrame,因为切片的本质还是列表
# 相当于 行列都用列表索引
df.iloc[:2,1:3]
age sex
student1 15 1
student2 20 0

数据过滤和筛选

# 因为 df.iloc[:2,1:3] 返回是一个 DataFrame 因此可以继续根据 bool 方式进行过滤
df.iloc[:2,1:3][df.sex == 0]
D:\Anaconda3\envs\data\lib\site-packages\ipykernel_launcher.py:2: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
age sex
student2 20 0
df.loc['student1':'student3',['age','sex']][df.sex==1]
D:\Anaconda3\envs\data\lib\site-packages\ipykernel_launcher.py:1: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  """Entry point for launching an IPython kernel.
age sex
student1 15 1
student3 18 1

你可能感兴趣的:(日常学习,Python学习笔记,Python数据分析与挖掘,python,学习,数据挖掘)