二、Pandas-6.索引、选择与过滤

"""
    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方法      根据行和列的标签设置单个值 

"""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值