A B C D
123 0.132003 -0.827317 -0.076467 True
124 1.130127 -1.436737 -1.413681 True
125 1.024180 0.569605 0.875906 True
126 0.974466 -2.006747 -0.410001 False
127 0.545952 -1.219217 -1.226825 False
128 -1.281247 -0.727707 -0.121306 True
嗨,我如何在 pandas 数据框中找到给定 bool(即本例为 False)从给定索引(即本例为 123)开始的第一次出现?最后返回该特定行。
最佳答案
你可以试试idxmin
:
>>> df['D'].idxmin()
126
>>>
对于真值你可以做idxmax
:
>>> df['D'].idxmax()
123
>>>
但是假设 D
列是这样的:
>>> df['D']
123 foo
124 bar
125 foo
126 bar
127 foo
128 bar
Name: D, dtype: object
然后你想要第一个bar
,你可以这样做:
>>> df['D'].eq('bar').idxmax()
124
>>>
关于python - 如何从索引开始查找第一次出现的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68991574/