鱼C论坛

 找回密码
 立即注册
查看: 2526|回复: 0

[技术交流] Series的index

[复制链接]
发表于 2020-12-9 17:16:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在pandas里对Series的各个位置、标签上的数据的访问,
可以通过loc、iloc、at、iat来访问。
带i的一般是通过位置相关得到数据,不带i的通过标签label来获得对应数据
这里的loc、iloc等不是函数,可以理解为index的属性。

  1. import pandas as pd
  2. import numpy as np
  3. val = np.array([2, 4, 5, 6])
  4. ii = range(10, 14)
  5. s0 = pd.Series(val)
  6. s1 = pd.Series(val, index=ii)
  7. idx =  "hello the cruel world".split()
  8. t = pd.Series(val, index=idx)
  9. print("s0", "*" * 11)
  10. print(s0.iloc[0])
  11. print(s0.iat[3])
  12. print(s0.loc[0])
  13. print(s0.at[3])
  14. print("s2", "*" * 11)
  15. print(s1.iloc[1])
  16. print(s1.iat[2])
  17. print(s1.loc[11])
  18. print(s1.at[12])
  19. #print s1.iloc[11]#wrong
  20. #print s1.iat[12]
  21. #print s1.loc[1] #wrong
  22. #print s1.at[2]
  23. print ("t", "*" * 12)
  24. print(t.iloc[0])
  25. print (t.iat[2])
  26. print (t.loc["hello"])
  27. print (t.at["cruel"])
复制代码


输出结果:
s0 ***********
2
6
2
6
s2 ***********
4
5
4
5
t ************
2
5
2
5
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-30 18:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表