python - 字典如 get() 在 pandas Series 上,索引值如 iloc
问题描述
假设我有以下熊猫系列:
series=pd.Series(data=['A', 'B'], index=[2,3])
.iloc
我可以使用as获得第一个值series.iloc[0] #output: 'A'
或者,我可以使用get
传递实际索引值的方法,例如series.get(2) #output: 'A'
但是有什么方法可以iloc
像索引一样传递给get
方法来获取系列中的第一个值?
>>> series.get(0, '')
'' # Expected 'A', the first value for index 0
一种方法是仅重置索引并将其删除然后调用get
方法:
>>> series.reset_index(drop=True).get(0, '')
'A'
是否有任何其他类似于get
方法(或使用方法的get
方法)的函数允许传递iloc
类似的索引,而无需重置索引的开销?
编辑:请注意,这个系列来自数据框屏蔽,所以这个系列可以是空的,这就是为什么我故意添加默认值作为方法的空''
字符串get
。
解决方案
这是你的想法还是不是真的?
series.get(series.index[0])
推荐阅读
- python - 存储相关矩阵的上/下半部分
- android - NotifyItemRemoved 使用 firebaserecycleradapter 无法正常工作?
- javascript - 反应路由器投掷
外面 错误 - javascript - 获取最后一个子元素之前的内部文本
- jmeter - Jmeter 线程没有减速
- react-native - 如何在本机反应中分离2个图像
- go - jfrog 带参数的 go 命令
- amazon-web-services - 如何获取 EC2 实例的实例名称、cpu 数量、内核和操作系统信息
- android - 如何在 Espresso Android 中为每个单独的测试用例重新启动应用程序
- postgresql - postgresql:根据两个不同时间戳列与 create_time 的计数创建图形