0

I turned a panda df to np array by
df2.to_numpy()
it returns an array like:

array([['040F', '040J', nan, nan, nan, nan, nan, nan],
       ['040F', '040J', nan, nan, nan, nan, nan, nan],
       ['040F', '040J', nan, nan, nan, nan, nan, nan],
       ['040V', '007', nan, nan, nan, nan, nan, nan],
       ['040S', '040J', '040F', nan, nan, nan, nan, nan])

Note: the array "width" is not fixed
Now I get the column number which contains "040J" by

df2.columns[np.nonzero(df2.to_numpy() == "040J")[-1]]
Index(['V2', 'V2', 'V2', 'V2'],
      dtype='object', name=0)

"V2" is the column label which contains "040J" in df2
this results skipped 4th row since it contains no "040J"

This array is OK for me, but I need to now which row does not contain "040J", how can I do it?

2
  • why not use pandas directly? Commented Feb 8, 2021 at 1:30
  • I wish there is a way to do it, but just fail to find a method using pandas alone and produces "a list of column# of each row's 040J" Commented Feb 8, 2021 at 23:32

2 Answers 2

2

As Ferris showed you can do it directly in pandas, but if you prefer numpy you can do it like this:

a = df2.to_numpy()
(~(a=='040J').any(1)).nonzero()
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, this also works, but the result: (array([3], dtype=int64),) is a tuple, is there a way to retrieve the number "3"?
nonzero returns a tuple of arrays, one for each dimension. As we know that we have exactly one dimension we can safely use ...[0]. To get the first element of the array you'll have to wrap it in a try block in case all rows contain '040J': try: i = (~(a=='040J').any(1)).nonzero()[0][0] except: i = None
2
# which column contains '040J'
obj = (df2 == "040J").sum()
obj[obj > 0]

# which row does not contains '040J'
obj = (df2 == "040J").sum(1)
obj[obj == 0]

2 Comments

Thanks, this is beyond my knowledge but neat, only I don't know about the result: 4 0 dtype: int64 What is 4 0? The return is actually 0, an int64 data, how do I get the 4?
that mean's 4th rows has 0 040J , you can get 4 from obj[obj == 0].index.tolist()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.