nithin
nithin

Reputation: 781

Pandas method to apply a function on the entire DataFrame

I have created a function where I need to pass data frame I get shape and return me df.head()

    def shape_of_df(df):
        tup = df.shape
        print('Shape of df  is Rows :{0[0]},column:{0[1]}'.format(tup))
        return df.head()

Now when I call function with items.apply(shape_of_df) I get error however if I call function and pass df as parameter it works fine. Please let me know I am missing something very basic when I use df.apply() couldn't figure out in docs

   shape_of_df(items) # this works fine prints shape and df.head()

Upvotes: 0

Views: 2400

Answers (1)

cs95
cs95

Reputation: 402483

The first line of the docs explains that DataFrame.apply will "Apply a function along an axis of the DataFrame" (i.e., along the rows or along the columns).

You want to apply a function on the entire dataFrame instead, so if you're trying to chain functions, you should look into DataFrame.pipe. The usage is similar:

items.pipe(shape_of_df)

As long as you satisfy the condition that shape_of_df accepts a DataFrame as input and correspondingly returns one as output.

Upvotes: 3

Related Questions