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

1、DataFrame 索引 1.1 普通索引取值
【python|python3-pandas DataFrame 索引、bool索引、pandas 字符串方法】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) """ wxyz a0123 b4567 c891011 """ print(t1[:2]) """ wxyz a0123 b4567 """ print(t1[:2]["x"]) """ a1 b5 Name: x, dtype: int32 """ print(t1["y"]) """ a2 b6 c10 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) """ wxyz a0123 b4567 c891011 """ print(t3.loc["a", "z"])# 3a 行 z 列 print(type(t3.loc["a", "z"]))# # 取第 b 行 print(t3[1:2]) print(t3.loc["a"]) print(t3.loc["a", :]) """ wxyz b4567 w0 x1 y2 z3 Name: a, dtype: int32 w0 x1 y2 z3 Name: a, dtype: int32 """

取列
# 取第 y 列 print(t3["y"]) print(t3.loc[:,"y"]) """ a2 b6 c10 Name: y, dtype: int32 a2 b6 c10 Name: y, dtype: int32 """

取 多行 多列
print(t3.loc[["a","b"], ["w", "z"]]) """ wz a03 b47 """ print(t3.loc["a":"c", ["w", "z"]])# 注意 c 行被选中了 """ wz a03 b47 c811 """ print(t3.loc[["a","b"]]) """ wxyz a0123 b4567 """ print(t3.loc[:, ["w", "z"]]) """ wz a03 b47 c811 """

1.3 DataFrame.iloc 通过位置获取行数据
取行
print(t3) """ wxyz a0123 b4567 c891011 """ print(t3.iloc[1])# 取行 """ w4 x5 y6 z7 Name: b, dtype: int32 """

取列
print(t3.iloc[:, 1]) """ a1 b5 c9 Name: x, dtype: int32 """# 取多列 print(t3.iloc[:, [2,1]]) """ yx a21 b65 c109 """

取多行 多列
print(t3.iloc[[0,2], [2,1]]) """ yx a21 c109 """ print(t3.iloc[1:,:2]) """ wx b45 c89 """ t3.iloc[1:,:2] = 30 print(t3) """ wxyz a0123 b303067 c30301011 """

2、DataFrame bool索引
print(t3) """ wxyz a0123 b303067 c30301011 """ print(t3[t3["y"] > 3]) """ wxyz b303067 c30301011 """ print(t3[(t3["y"] > 3) & (t3["y"]<20)]) """ wxyz b303067 c30301011 """ print(t3[(t3["y"] > 3) |(t3["y"]<20)]) """ wxyz a0123 b303067 c30301011 """

3、pandas 字符串方法 python|python3-pandas DataFrame 索引、bool索引、pandas 字符串方法
文章图片

data = https://www.it610.com/article/[['Google',10],['Runoob',12],['Wiki',13]] df = pd.DataFrame(data,columns=['Site','Age']) print(df) """ SiteAge 0Google10 1Runoob12 2Wiki13 """ print(df[df["Site"].str.len()>4]) """ SiteAge 0Google10 1Runoob12 """ 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

    推荐阅读