import pandas as pd
1. 直接索引
df = pd.DataFrame({'AdmissionDate': ['2021-01-25','2021-01-22','2021-01-20',
'2021-01-18','2021-01-17','2021-01-17','2021-01-21'],
'StudentID': [7,1,3,2,6,3,4],
'Name': ['Jack','Shyam','Mohan','Janne','Lucky','Abhinav','Danny'],
'Stream':['CSE','ECE','Civil','Mechanical','CSE','IT','EEE']
})
df.set_index("Name",inplace=True)
# 选取某一列或者某几列
print(df["AdmissionDate"])
print(df[["AdmissionDate","StudentID"]]) # 选取多列时,多个列要放到一个list中
Name
Jack 2021-01-25
Shyam 2021-01-22
Mohan 2021-01-20
Janne 2021-01-18
Lucky 2021-01-17
Abhinav 2021-01-17
Danny 2021-01-21
Name: AdmissionDate, dtype: object
AdmissionDate StudentID
Name
Jack 2021-01-25 7
Shyam 2021-01-22 1
Mohan 2021-01-20 3
Janne 2021-01-18 2
Lucky 2021-01-17 6
Abhinav 2021-01-17 3
Danny 2021-01-21 4
# 按行编号选取连续的行
df[1:3] # df[start:end], [start,end)闭开区间
|
AdmissionDate |
StudentID |
Stream |
Name |
|
|
|
Shyam |
2021-01-22 |
1 |
ECE |
Mohan |
2021-01-20 |
3 |
Civil |
# 按行时间索引选取连续的行
df["AdmissionDate"] = pd.to_datetime(df["AdmissionDate"])
df.set_index("AdmissionDate",inplace=True)
# df["2021-01-01":"2021-01-20"] # 将被弃用
df.sort_index().loc["2021-01-01":"2021-01-20",:] # 推荐写法
|
StudentID |
Stream |
AdmissionDate |
|
|
2021-01-17 |
6 |
CSE |
2021-01-17 |
3 |
IT |
2021-01-18 |
2 |
Mechanical |
2021-01-20 |
3 |
Civil |
2. 布尔索引
df = pd.DataFrame({'AdmissionDate': ['2021-01-25','2021-01-22','2021-01-20',
'2021-01-18','2021-01-17','2021-01-17','2021-01-21'],
'StudentID': [7,1,3,2,6,3,4],
'Name': ['Jack','Shyam','Mohan','Janne','Lucky','Abhinav','Danny'],
'Stream':['CSE','ECE','Civil','Mechanical','CSE','IT','EEE']
})
df["AdmissionDate"] = pd.to_datetime(df["AdmissionDate"])
df.set_index("Name",inplace=True)
df
|
AdmissionDate |
StudentID |
Stream |
Name |
|
|
|
Jack |
2021-01-25 |
7 |
CSE |
Shyam |
2021-01-22 |
1 |
ECE |
Mohan |
2021-01-20 |
3 |
Civil |
Janne |
2021-01-18 |
2 |
Mechanical |
Lucky |
2021-01-17 |
6 |
CSE |
Abhinav |
2021-01-17 |
3 |
IT |
Danny |
2021-01-21 |
4 |
EEE |
# 选取满足某一条件的行
df[df["StudentID"]==2]
|
AdmissionDate |
StudentID |
Stream |
Name |
|
|
|
Janne |
2021-01-18 |
2 |
Mechanical |
# 选取满足多个条件的行
# 注意:索引列表中,可以使用& |操作符,但不能使用and or not等关键字
from datetime import datetime
df[(df["StudentID"]>=3) & (df["AdmissionDate"]>="2021-01-20")] # 注意:索引列表中,各布尔条件必须用圆括号扩起来
2021-01-20 00:00:00
|
AdmissionDate |
StudentID |
Stream |
Name |
|
|
|
Jack |
2021-01-25 |
7 |
CSE |
Mohan |
2021-01-20 |
3 |
Civil |
Danny |
2021-01-21 |
4 |
EEE |
df[(df["StudentID"]>=3) | (df["AdmissionDate"]>="2021-01-20")]
|
AdmissionDate |
StudentID |
Stream |
Name |
|
|
|
Jack |
2021-01-25 |
7 |
CSE |
Shyam |
2021-01-22 |
1 |
ECE |
Mohan |
2021-01-20 |
3 |
Civil |
Lucky |
2021-01-17 |
6 |
CSE |
Abhinav |
2021-01-17 |
3 |
IT |
Danny |
2021-01-21 |
4 |
EEE |
3. 索引器索引
Dataframe的loc和iloc属性
- loc属性:
- 以列名和行名作为参数,当只有一个参数时,默认是行名,即抽取整行数据,包括所有列
- iloc属性:
- 以行和列位置索引,作为参数。当只有一个参数时,默认是行索引,即抽取整行数据,包括所有列
df = pd.DataFrame({'AdmissionDate': ['2021-01-25','2021-01-22','2021-01-20',
'2021-01-18','2021-01-17','2021-01-17','2021-01-21'],
'StudentID': [7,1,3,2,6,3,4],
'Name': ['Jack','Shyam','Mohan','Janne','Lucky','Abhinav','Danny'],
'Stream':['CSE','ECE','Civil','Mechanical','CSE','IT','EEE']
})
df["AdmissionDate"] = pd.to_datetime(df["AdmissionDate"])
df.set_index("Name",inplace=True)
df
|
AdmissionDate |
StudentID |
Stream |
Name |
|
|
|
Jack |
2021-01-25 |
7 |
CSE |
Shyam |
2021-01-22 |
1 |
ECE |
Mohan |
2021-01-20 |
3 |
Civil |
Janne |
2021-01-18 |
2 |
Mechanical |
Lucky |
2021-01-17 |
6 |
CSE |
Abhinav |
2021-01-17 |
3 |
IT |
Danny |
2021-01-21 |
4 |
EEE |
3.1 loc索引器
# 选取一行, loc["行索引名称"]
df.loc["Jack"]
AdmissionDate 2021-01-25 00:00:00
StudentID 7
Stream CSE
Name: Jack, dtype: object
# df.loc[['行1,行2'],['列1,列2']]:选取行列组合
df.loc[["Jack","Janne"],["StudentID","Stream"]]
|
StudentID |
Stream |
Name |
|
|
Jack |
7 |
CSE |
Janne |
2 |
Mechanical |
# df.loc[(df['列']>条件)]:按条件选取列满足一定条件的行。
df.loc[df["StudentID"]>=2,["Stream","AdmissionDate"]]
|
Stream |
AdmissionDate |
Name |
|
|
Jack |
CSE |
2021-01-25 |
Mohan |
Civil |
2021-01-20 |
Janne |
Mechanical |
2021-01-18 |
Lucky |
CSE |
2021-01-17 |
Abhinav |
IT |
2021-01-17 |
Danny |
EEE |
2021-01-21 |
# df.loc[行1:行2,列1:列2]:按列名选取连续的列。冒号前后留空代表开口。
df.loc["Jack":"Janne","AdmissionDate":"StudentID"]
|
AdmissionDate |
StudentID |
Name |
|
|
Jack |
2021-01-25 |
7 |
Shyam |
2021-01-22 |
1 |
Mohan |
2021-01-20 |
3 |
Janne |
2021-01-18 |
2 |
3.2 iloc索引器
iloc索引器与loc索引器的使用几乎相同,唯一不同的是,iloc索引器中只能使用原始索引,不能使用自定义索引。
注意:原始索引初值从0开始,切片前闭后开。自定义索引切片为闭区间
df.iloc[1:3,1:2]
|
StudentID |
Name |
|
Shyam |
1 |
Mohan |
3 |
带步长的索引
df.iloc[::2]