zerohedge
zerohedge

Reputation: 3765

pandas: Apply operation to all columns without applymap?

I’m trying to figure out if it’s possible to achieve the following without using apply and without using a for loop.

df= [df[x].map(lambda x: len(x) > 5) for x in df.columns]

I’m specifically trying to avoid apply and applymap, and look for a vectorised solution. All values in DF are strings. I’m using the above as a mask later on.

The fastest I've found is:

df1 = [df[x].map(lambda x: len(x) > 5) for x in df.columns]
df2 = df[pd.concat(df1, axis=1, keys=[s.name for s in df1]).any(1)]

It's faster than:

df[(df.applymap(len) > 5).any(axis=1)]

Upvotes: 0

Views: 86

Answers (1)

BENY
BENY

Reputation: 323326

How about vectorize, at least it should be slightly faster than apply , about the comparision of for loop , it all depends on your data size and shape . Link, Link

np.vectorize(len)(df.values)>5

Upvotes: 4

Related Questions