Pandas - 'Series' object has no attribute
Asked Answered
G

2

29

I need to use a lambda function to do a row by row computation. For example create some dataframe

import pandas as pd
import numpy as np

def myfunc(x, y):
    return x + y

colNames = ['A', 'B']
data = np.array([np.arange(10)]*2).T

df = pd.DataFrame(data, index=range(0, 10), columns=colNames)

using 'myfunc' this does work

df['D'] = (df.apply(lambda x: myfunc(x.A, x.B), axis=1))

but this second case does not work!

df['D'] = (df.apply(lambda x: myfunc(x.colNames[0], x.colNames[1]), axis=1))

giving the error

AttributeError: ("'Series' object has no attribute 'colNames'", u'occurred at index 0')

I really need to use the second case (access the colNames using the list) which gives an error, any clues on how to do this?

Gimcrackery answered 9/11, 2016 at 11:53 Comment(0)
M
28

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)) 
Metope answered 9/11, 2016 at 12:7 Comment(0)
L
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.

Lemmuela answered 5/9, 2022 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.