TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas Apply

Pandas Apply with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas DataFrame.apply()

The Pandas apply() function allows the user to pass a function and apply it to every single value of the Pandas series. This function improves the capabilities of the panda's library because it helps to segregate data according to the conditions required. So that it can be efficiently used for data science and machine learning.

The objects that are to be passed to function are Series objects whose index is either the DataFrame's index, i.e., axis=0 or the DataFrame's columns, i.e., axis=1. By default, the result_type=None and the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type argument.

Syntax:

DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)

Parameters:

  • func: It is a function that is to be applied to each column or row.
  • axis: {0 or 'index', 1 or 'columns'}, default value 0
    It is an axis along which the function is applied. It can have two values:
    • 0 or 'index': It applies the function to each of the columns.
    • 1 or 'columns': It applies the function to each of the rows.
  • broadcast: It is an optional parameter that returns the boolean values.
    Only relevant for aggregation functions:
    False or None: It returns a Series whose length will be the length of the index or the number of columns based on the axis parameter.
    True: The results will be broadcasted to the original shape of the frame; the original index and columns will be retained.
  • raw: bool, default value False
    False: It passes each row or column as a Series to the function.
    True: The passed function will receive a ndarray objects. If you are applying a NumPy reduction function, it will achieve better performance.
  • reduce: bool or None, default value None
    It tries to apply the reduction procedures. If the DataFrame is empty, the apply will use the reduce to determine whether the result should be a Series or a DataFrame.
    By default, reduce=None, the apply's return value will be guessed by calling func on an empty Series (note: All the exceptions that are to be raised by func will be ignored while guessing). If reduce=True, Series will always be returned, whereas reduce=False, Always the DataFrame will be returned.
  • result_type: {'expand', 'reduce', 'broadcast', None}, default value None
    These only act when axis=1 (columns):
    'expand': It defines the list-like results that will be turned into columns.
    'reduce': It is the opposite of 'expand'. If possible, it returns a Series rather than expanding list-like results.
    'broadcast': It broadcast the results to the original shape of the DataFrame, the original index, and the columns will be retained.
    The default value None depends on the return value of the applied function , i.e., list-like results returned as a Series of those.
    If apply returns a Series, it expands to the columns.
  • args: It is a positional argument that is to be passed to func in addition to the array/series.
  • **kwds: It is an optional keyword argument, which is used to pass as keywords arguments to func.

Returns:

It returns the result of applying func along the given axis of the DataFrame.

Example:

info = pd.DataFrame([[2, 7]] * 4, columns=['P', 'Q'])
info.apply(np.sqrt)
info.apply(np.sum, axis=0)
info.apply(np.sum, axis=1)
info.apply(lambda x: [1, 2], axis=1)
info.apply(lambda x: [1, 2], axis=1, result_type='expand')
info.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1)
info.apply(lambda x: [1, 2], axis=1, result_type='broadcast')
info

Output

     A     B
0    2     7
1    2     7
2    2     7
3    2     7





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf