Usage of python apply function

Python keyword arguments


Usage of python apply function

The function format is: apply(func,*args,**kwargs)

Purpose: When the parameters of a function exist in a tuple or a dictionary, it is used to indirectly call the function, and the parameters in the tuple or dictionary are passed to the parameters in order

Analysis: args is a tuple containing the positional parameters passed according to the parameters required by the function. must be passed in strict accordance with the positional order of this parameter (a=3, b=4), not in the order of (b=4, a=3)
kwargs is a dictionary containing keyword arguments, and args If not passed, kwargs need to be passed, must be left empty in the position of args

The return value of apply is the return value of the function func

  1. def function(a,b):    
  2.     print(a,b)    
  3. apply(function,('good','better'))    
  4. apply(function,(2,3+6))    
Output result:

('good', 'better')

(2, 9)


Python  filter() function

grammar

Following is the syntax of filter() method:

filter(function, iterable)

parameter

  • function – Judgment function.
  • iterable – an iterable object.

return value

Back to list.

python -- lambda expression

Lambda expressions are usually used when you need a function, but you don't want to bother to name a function , that is, anonymous functions .

The content of the anonymous function represented by lambda should be very simple. If it is complicated, simply redefine a function. Using lambda is a bit too stubborn.

Lambda is used to define an anonymous function. If you want to bind a name to it, it will be a bit superfluous. Usually, the lambda function is used directly. As follows:

add = lambda x, y : x+y add(1,2) # 结果为3
Python axis

The top voted answer reveals the essence of the problem:

In fact, there is a problem with understanding the axis. df.mean actually takes the mean of all columns on each row, rather than retaining the mean of each column. Maybe a simple way to remember is that axis=0 means to go across the row ( down) , and axis=1 means across the column (across) , as an adverb of the method action (Translator's Note)

in other words:

  • Use a value of 0 to execute the method down each column or row label\index value
  • Use a value of 1 to execute the corresponding method along each row or column label modulo

The following figures represent the meanings of axis 0 and 1 in the DataFrame:


Diagram of the action direction of the axis parameter

Also, keep in mind that Pandas maintains Numpy's usage of the keyword axis, which is explained in the Numpy library's glossary:

Axes are used to define properties for arrays with more than one dimension. Two-dimensional data has two axes: the 0th axis runs vertically down the rows, and the 1st axis runs horizontally along the columns.

So the first column in the question, df.mean(axis=1), represents the calculation of the mean along the horizontal direction of the column, and the second column, df.drop(name, axis=1), represents the column label(s) corresponding to the name. Delete in turn in the horizontal direction.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325718029&siteId=291194637