Python Forum
[pandas] Find the first element that is -1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pandas] Find the first element that is -1
#4
You can do this very fast with numpy
import numpy as np

a = np.array([[58,  68,  58,   59,   -1, -1], 
              [59,  69,  59,   -1,   -1, -1], 
              [93,  94,  93,   33,   -1, -1], 
              [58,  59,  58,   68,   -1, -1], 
              [92,  94,  92,   33,   -1, -1]])

print(np.sum((a != -1), axis=1))
which outputs a list with the "length" of each row
Quote:[4 3 4 4 4]
Reply


Messages In This Thread
RE: Find the first element that is -1 - by stullis - Jun-13-2019, 04:47 PM
RE: Find the first element that is -1 - by ThomasL - Jun-14-2019, 04:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Referring to a specific element in Pandas Dataframe Helmi 2 3,306 Mar-17-2019, 09:12 PM
Last Post: Helmi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020