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
Answer from Javide on Stack OverflowCheck 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 --]
'Series' object has no attribute 'to_numpy' - Courses - Educative
python - AttributeError: 'DataFrame' object has no attribute 'series' in pandas - Stack Overflow
AttributeError: 'Series' object has no attribute 'as_matrix'
python - Pandas - 'Series' object has no attribute - Stack Overflow
When you use df.apply(), each row of your DataFrame will be passed to your lambda function as a pandas Series. The frame's columns will then be the index of the series and you can access values using series[label].
So this should work:
df['D'] = (df.apply(lambda x: myfunc(x[colNames[0]], x[colNames[1]]), axis=1))
In general, this error occurs if you try to access an attribute that doesn't exist on an object. For pandas Serieses (or DataFrames), it occurs because you tried to index it using the attribute access (.).
In the case in the OP, they used x.colNames[0] to access the value on colNames[0] in row x but df doesn't have attribute colNames, so the error occurred.1
Another case this error may occur is if an index had a white space in it that you didn't know about. For example, the following case reproduces this error.
s = pd.Series([1, 2], index=[' a', 'b'])
s.a
In this case, make sure to remove the white space:
s.index = [x.strip() for x in s.index]
# or
s.index = [x.replace(' ', '') for x in s.index]
Finally, it's always safe to use [] to index a Series (or a DataFrame).
1: Serieses have the following attributes: axes, dtypes, empty, index, ndim, size, shape, T, values. DataFrames have all of these attributes + columns. When you use df.apply(..., axis=1), it iterates over the rows where each row is a Series whose indices are the column names of df.
Try df.values instead. This will have the same effect for versions of pandas prior to 0.24.0
This feature was just added in Version 0.24.0, which was released a couple of days ago. If you haven't updated yet, the attribute doesn't exist! Once you update pandas the problem should resolve itself.
import pandas as pd
tables = pd.read_html("https://www.sec.gov/Archives/edgar/data/949012/000156761919015285/xslForm13F_X01/form13fInfoTable.xml")
len(tables)
ren=tables[3]
ren.drop(ren.index[[0,1,2]], inplace=True)
ren[3] = pd.to_numeric(ren[3], errors='coerce')
ren.sort_values([3],ascending=False, inplace=True)
ren
0 1 2 3 ...
101 JPMorgan COM 46625h100 48532 ...
44 Cisco COM 17275r102 47376 ...
204 Waste Management COM 94106L109 41558 ...
117 Microsoft COM 594918104 37492 ...
99 Johnson & Johnson COM 478160104 31491 ...
Do this:
pd.to_numeric(ren[3], errors='coerce')