According to the pandas documentation, Series.nonzero() is replaced by Series.to_numpy().nonzero().
Instead of
df.iloc[0].nonzero()
Try
df.iloc[0].to_numpy().nonzero()
Answer from Sabid Habib on Stack Overflowpython - pandas: print names of columns with nonzero elements in row? - Stack Overflow
Error in milo.make_nhoods with 'Series' object has no attribute 'nonzero'
AttributeError: 'Series' object has no attribute [X] when preparing DataBlock - Part 1 (2020) - fast.ai Course Forums
python - How to fix AttributeError: 'Series' object has no attribute 'to_numpy' - Stack Overflow
Check the version of your pandas library:
import pandas
print(pandas.__version__)
If your version is less than 0.24.1:
pip install --upgrade pandas
If you need your code to work with all versions of pandas, here's a simple way to convert a Series into a NumPy array:
import pandas as pd
import numpy as np
s = pd.Series([1.1, 2.3])
a = np.array(s)
print(a) # [1.1 2.3]
On an advanced note, if your Series has missing values (as NaN values), these can be converted to a masked array:
s = pd.Series([1.1, np.nan])
a = np.ma.masked_invalid(s)
print(a) # [1.1 --]
As stated in another answer, the as_matrix method is deprecated since 0.23.0, so you should use to_numpy instead. However, I want to highlight the fact that as_matrix and to_numpy have different signatures: as_matrix takes a list of column names as one of its parameter, in case you want to limit the conversion to a subset of the original DataFrame; to_numpy does not accept such a parameter. As a consequence, the two methods are completely interchangeable only if you want to convert the DataFrame in full. If you (as in my case) need to convert a subset of the matrix, the usage would be quite different in the two use cases.
For example let's assume we only need to convert the subset ['col1', 'col2', 'col4'] of our original DataFrame to a Numpy array. In that case you might have some legacy code relying on as_matrix to convert, which looks more or less like:
df.as_matrix(['col1', 'col2', 'col4'])
While converting the above code to to_numpy you cannot simply replace the function name like in:
df.to_numpy(['col1', 'col2', 'col4']) # WRONG
because to_numpy does not accept a subset of columns as parameter. The solution in that case would be to do the selection first, and apply to_numpy to the result, as in:
df[['col1', 'col2', 'col4']].to_numpy() # CORRECT
The purpose of as_matrix method is to
Convert the frame to its Numpy-array representation.
as_matrix method is deprecated since 0.23.0
0.25.1 documentation says : Deprecated since version 0.23.0: Use DataFrame.values() instead
The two alternatives are
- .values() : Returns numpy.ndarray
- .to_numpy() : Returns numpy.ndarray
However, .values() documentation gives another warning :- Warning We recommend using DataFrame.to_numpy() instead.
I got the error in a slightly different way : AttributeError: 'DataFrame' object has no attribute 'as_matrix'