python3-pandas DataFrame 索引、bool索引、pandas 字符串方法

1、DataFrame 索引

1.1 普通索引取值

pandas 取行或者列的注意点:

  • 方括号写数组,表示取行,对行进行操作
  • 方括号写字符串,表示取列,对列进行操作
import pandas as pd
import numpy as np
# pandas 取行或者列的注意点
# 方括号写数组,表示取行,对行进行操作
# 方括号写字符串,表示取列,对列进行操作
t1 = pd.DataFrame(np.arange(12).reshape(3,4), index=list("abc"), columns=list("wxyz"))
print(t1)
"""
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
"""
print(t1[:2])
"""
   w  x  y  z
a  0  1  2  3
b  4  5  6  7
"""
print(t1[:2]["x"])
"""
a    1
b    5
Name: x, dtype: int32
"""
print(t1["y"])
"""
a     2
b     6
c    10
Name: y, dtype: int32
"""

1.2 DataFrame.loc 通过标签索引行数据

取行

t3 = pd.DataFrame(np.arange(12).reshape(3,4), index=list("abc"), columns=list("wxyz"))
print(t3)
"""
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
"""
print(t3.loc["a", "z"])  # 3   a 行 z 列
print(type(t3.loc["a", "z"]))  # <class 'numpy.int32'>
# 取第 b 行
print(t3[1:2])
print(t3.loc["a"])
print(t3.loc["a", :])
"""
   w  x  y  z
b  4  5  6  7
w    0
x    1
y    2
z    3
Name: a, dtype: int32
w    0
x    1
y    2
z    3
Name: a, dtype: int32
"""

取列

# 取第 y 列
print(t3["y"])
print(t3.loc[:,"y"])
"""
a     2
b     6
c    10
Name: y, dtype: int32
a     2
b     6
c    10
Name: y, dtype: int32
"""

取 多行 多列

print(t3.loc[["a","b"], ["w", "z"]])
"""
   w  z
a  0  3
b  4  7
"""
print(t3.loc["a":"c", ["w", "z"]])  # 注意 c 行被选中了
"""
   w   z
a  0   3
b  4   7
c  8  11
"""
print(t3.loc[["a","b"]])
"""
   w  x  y  z
a  0  1  2  3
b  4  5  6  7
"""
print(t3.loc[:, ["w", "z"]])
"""
   w   z
a  0   3
b  4   7
c  8  11
"""

1.3 DataFrame.iloc 通过位置获取行数据

取行

print(t3)
"""
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
"""
print(t3.iloc[1])  # 取行
"""
w    4
x    5
y    6
z    7
Name: b, dtype: int32
"""

取列

print(t3.iloc[:, 1])
"""
a    1
b    5
c    9
Name: x, dtype: int32
"""

# 取多列
print(t3.iloc[:, [2,1]])
"""
    y  x
a   2  1
b   6  5
c  10  9
"""

取多行 多列

print(t3.iloc[[0,2], [2,1]])
"""
    y  x
a   2  1
c  10  9
"""
print(t3.iloc[1:,:2])
"""
   w  x
b  4  5
c  8  9
"""
t3.iloc[1:,:2] = 30
print(t3)
"""
    w   x   y   z
a   0   1   2   3
b  30  30   6   7
c  30  30  10  11
"""

2、DataFrame bool索引

print(t3)
"""
    w   x   y   z
a   0   1   2   3
b  30  30   6   7
c  30  30  10  11
"""
print(t3[t3["y"] > 3])
"""
    w   x   y   z
b  30  30   6   7
c  30  30  10  11
"""
print(t3[(t3["y"] > 3) & (t3["y"]<20)])
"""
    w   x   y   z
b  30  30   6   7
c  30  30  10  11
"""
print(t3[(t3["y"] > 3) |(t3["y"]<20)])
"""
    w   x   y   z
a   0   1   2   3
b  30  30   6   7
c  30  30  10  11
"""

3、pandas 字符串方法

在这里插入图片描述

data = [['Google',10],['Runoob',12],['Wiki',13]]
df = pd.DataFrame(data,columns=['Site','Age'])
print(df)
"""
     Site  Age
0  Google   10
1  Runoob   12
2    Wiki   13
"""
print(df[df["Site"].str.len()>4])
"""
     Site  Age
0  Google   10
1  Runoob   12
"""
print(df["Site"].str.split("o"))
"""
0    [G, , gle]
1    [Run, , b]
2        [Wiki]
Name: Site, dtype: object
"""
print(df["Site"].str.split("o").tolist())
"""
[['G', '', 'gle'], ['Run', '', 'b'], ['Wiki']]
"""

https://www.bilibili.com/video/BV1hx411d7jb?p=27
https://www.bilibili.com/video/BV1hx411d7jb?p=28
https://www.runoob.com/pandas/pandas-dataframe.html

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值