less than 1 minute read

You CANNOT access each row’s index via lambda x: x.index in DataFrame.apply(axis=1) because apply treats each row as a numpy object, not a Series. However you can use lambda x: x.name to access each row’s name, which happens to equal its index.

df.apply(axis=1, func=lambda x: func(x.index))  # Wrong!
df.apply(axis=1, func=lambda x: func(x.name))   # OK!

Another workaround is to use Index.map(). Note that this function does not preserve indices.

pd.Series(df.index.map(lambda x: func(x)), index=df.index)

Tags:

Categories:

Updated:

Comments