"""
Series的索引与Numpy数组索引的功能类似,只不过Series的索引值可以不仅仅是整数
"""
import pandas as pd
import numpy as np
obj = pd.Series(np.arange(4.), index=["Zero", "One", "Two", "Three"])
print(obj)
"""
Zero 0.0
One 1.0
Two 2.0
Three 3.0
dtype: float64
"""
# ▶知识点1:查
# 方式一:
print(obj["Zero"]) # 0.0
# 方式二:
print(obj[["One", "Two"]])
"""
One 1.0
Two 2.0
dtype: float64
"""
# 方式三:
print(obj[1]) # 1.0
# 方式四:
print(obj[[1, 3]])
"""
One 1.0
Three 3.0
dtype: float64
"""
# 方式五:
print(obj[2:4]) # ▶知识点2:Series的切片是【包含尾部】的,普通的Python则不包含
"""
Two 2.0
Three 3.0
dtype: float64
"""
# 方式六:
print(obj[obj < 2])
"""
Zero 0.0
One 1.0
dtype: float64
"""
# ▶知识点3:修改
obj["Two":"Three"] = 5
print(obj)
"""
Zero 0.0
One 1.0
Two 5.0
Three 5.0
dtype: float64
"""
# ▶知识点4:使用【单个值】或【序列】,可以从DataFrame中索引出一个或多个列
df_1 = pd.DataFrame(np.arange(16).reshape((4, 4)), columns=["A", "B", "C", "D"],
index=["XiaoMing", "XiaoHong", "XiaoHua", "XiaoYing"])
print(df_1)
"""
A B C D
XiaoMing 0 1 2 3
XiaoHong 4 5 6 7
XiaoHua 8 9 10 11
XiaoYing 12 13 14 15
"""
# ▶知识点5:查
# 方式一:
print(df_1["A"])
"""
XiaoMing 0
XiaoHong 4
XiaoHua 8
XiaoYing 12
Name: A, dtype: int32
"""
# 方式二:
print(df_1[["B", "A"]])
"""
B A
XiaoMing 1 0
XiaoHong 5 4
XiaoHua 9 8
XiaoYing 13 12
"""
# 方式三:
print(df_1["XiaoMing":"XiaoHua"])
"""
A B C D
XiaoMing 0 1 2 3
XiaoHong 4 5 6 7
XiaoHua 8 9 10 11
"""
# 方式四:
print(df_1[:2])
"""
A B C D
XiaoMing 0 1 2 3
XiaoHong 4 5 6 7
"""
# 方式五:
print(df_1[df_1["C"] > 5])
"""
A B C D
XiaoHong 4 5 6 7
XiaoHua 8 9 10 11
XiaoYing 12 13 14 15
"""
# 方式六:
# ▶知识点6:使用loc和iloc选择数据,loc显式索引,iloc隐式索引(0,1,2,...)
print(df_1.iloc[0])
print(df_1.loc["XiaoYing"])
print(df_1.iloc[:2, :3])
print(df_1.loc["XiaoHong":"XiaoYing", :"C"])
print(df_1.iloc[:2, [1, 3]])
print(df_1.loc["XiaoHua":"XiaoYing", ["A", "C"]])
print(df_1.iloc[:, :3][df_1.C > 5])
print(df_1.loc["XiaoHong":"XiaoYing", "A":"C"][df_1.A > 5])
# ▶知识点7:修改
df_1[df_1 < 5] = 0
print(df_1)
"""
A B C D
XiaoMing 0 0 0 0
XiaoHong 0 5 6 7
XiaoHua 8 9 10 11
XiaoYing 12 13 14 15
"""
"""
▶知识点8:
DataFrame索引选项
类型 描述
------------------------------------------------------------------------------------------
df[val] 从DataFrame中选择单列或列序列
特殊情况的便利:布尔数组(过滤行)、切片(切片行)或布尔值DataFrame
df.loc[val] 根据标签选择DataFrame的单行或多行
df.loc[:,val] 根据标签选择单列或多列
df.loc[val1,val2] 同时选择行和列中的一部分
df.iloc[where] 根据整数位置选择单行或多行
df.iloc[:,where] 根据整数位置选择单列或多列
df.iloc[where_i,where_j] 根据整数位置选择行和列
df.at[label_i,label_j] 根据行、列整数位置选择单个标量值
reindex方法 通过标签选择行或列
get_value,set_value方法 根据行和列的标签设置单个值
"""
二、Pandas-6.索引、选择与过滤
最新推荐文章于 2024-02-28 22:21:56 发布