我有一个如下所示的数据框:
1 2 3 4 5 6 7 8 9
0 2 1 -1 -2 -3 -2 -1 0 1
我需要返回值变为负值并再次变为正值的列。
与此问题类似,但列和行倒置: Pandas: select the first value which is not negative anymore, return the row
有什么简单的方法可以做到这一点吗?
预期的输出将返回其更改的列号,如下所示:
neg = 3
pos = 8
答案 0 :(得分:3)
StopIteration
+ SENTINEL = object() # Unique object.
list_of_ints = [1, 2, 3]
iterator = iter(list_of_ints)
while True:
element = next(iterator, SENTINEL)
if element is SENTINEL:
break
print(element)
print("done")
解决方案。
找到标志变化 pandas
numpy
位置:
np.sign(df1).diff().ne(0)
否定:
df = df.replace(0,np.inf)
df1 = df.T
t = df1[np.sign(df1).diff().ne(0)[1:]]
s = (t>0)
pos = [*filter(s[0].get, s.index)]
s = (t<0)
neg = [*filter(s[0].get, s.index)]
对于每一行:
['8']