python - 获取真实值的熊猫系列标签,而不将系列存储在临时变量中
问题描述
输入:
pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd'])
期望的结果:
['a', 'c'] # can be in pd.Series or np.array format, doesn't matter.
但我想要一个不将系列存储在临时变量中的解决方案,例如
s = pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd'])
s[s == True].index
将系列存储在 s 中,并使用它两次。
我正在寻找的解决方案就像
np.where(s)
但返回真实值的标签,而不是它们的整数索引。
笔记:
这个问题与这个问题类似,但更具体。
解决方案
我总是通过loc
pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd']).loc[lambda x : x].index
Index(['a', 'c'], dtype='object')
推荐阅读
- c++ - 不带引号的 LLDB 摘要字符串
- android - Android 位置 GPS 追踪
- imagemagick - 可以在 AWS Linux 2 中添加对 PHP 的 imagick webp 支持吗?
- php - 从 2 个数据透视表返回数据
- python - Python获取字符串的位置和接下来的X行
- javascript - Javascript 和 React:如何调用或引用函数?
- python-3.x - 我无法让这段代码工作,谁能告诉我我做错了什么?subl 说第 18 行有一个错误,但真的无法弄清楚
- python - python中的逻辑回归-初学者-重塑数据集
- vim - vim 中那些奇怪的标签“<++>”有什么用?
- facebook - 如何使用Oauth2连接和使用facebook营销api