Python | Pandas.apply() Last Updated : 21 Aug, 2024 Comments Improve Suggest changes 24 Likes Like Report Pandas.apply allow the users to pass a function and apply it on every single value of the Pandas series. It comes as a huge improvement for the pandas library as this function helps to segregate data according to the conditions required due to which it is efficiently used in data science and machine learning. Installation: Import the Pandas module into the python file using the following commands on the terminal: pip install pandas To read the csv file and squeezing it into a pandas series following commands are used: import pandas as pds = pd.read_csv("stock.csv", squeeze=True)Syntax:s.apply(func, convert_dtype=True, args=())Parameters:func: .apply takes a function and applies it to all values of pandas series. convert_dtype: Convert dtype as per the function's operation. args=(): Additional arguments to pass to function instead of series. Return Type: Pandas Series after applied function/operation. Example #1: The following example passes a function and checks the value of each element in series and returns low, normal or High accordingly. PYTHON import pandas as pd # reading csv s = pd.read_csv("stock.csv", squeeze = True) # defining function to check price def fun(num): if num<200: return "Low" elif num>= 200 and num<400: return "Normal" else: return "High" # passing function to apply and storing returned series in new new = s.apply(fun) # printing first 3 element print(new.head(3)) # printing elements somewhere near the middle of series print(new[1400], new[1500], new[1600]) # printing last 3 elements print(new.tail(3)) Output:Example #2: In the following example, a temporary anonymous function is made in .apply itself using lambda. It adds 5 to each value in series and returns a new series. PYTHON import pandas as pd s = pd.read_csv("stock.csv", squeeze = True) # adding 5 to each value new = s.apply(lambda num : num + 5) # printing first 5 elements of old and new series print(s.head(), '\n', new.head()) # printing last 5 elements of old and new series print('\n\n', s.tail(), '\n', new.tail()) Output:0 50.121 54.102 54.653 52.384 52.95Name: Stock Price, dtype: float64 0 55.121 59.102 59.653 57.384 57.95Name: Stock Price, dtype: float643007 772.883008 771.073009 773.183010 771.613011 782.22Name: Stock Price, dtype: float64 3007 777.883008 776.073009 778.183010 776.613011 787.22Name: Stock Price, dtype: float64 As observed, New values = old values + 5 Create Quiz Comment K Kartikaybhutani Follow 24 Improve K Kartikaybhutani Follow 24 Improve Article Tags : Misc Python python-modules Explore IntroductionPandas Introduction 3 min read How to Install Pandas in Python? 5 min read How To Use Jupyter Notebook - An Ultimate Guide 5 min read Creating ObjectsCreating a Pandas DataFrame 2 min read Python Pandas Series 5 min read Creating a Pandas Series 3 min read Viewing DataPandas Dataframe/Series.head() method - Python 3 min read Pandas Dataframe/Series.tail() method - Python 3 min read Pandas DataFrame describe() Method 4 min read Selection & SlicingDealing with Rows and Columns in Pandas DataFrame 3 min read Pandas Extracting rows using .loc[] - Python 3 min read Extracting rows using Pandas .iloc[] in Python 7 min read Indexing and Selecting Data with Pandas 4 min read Boolean Indexing in Pandas 6 min read Python | Pandas DataFrame.ix[ ] 2 min read Python | Pandas Series.str.slice() 3 min read How to take column-slices of DataFrame in Pandas? 2 min read OperationsPython | Pandas.apply() 4 min read Apply function to every row in a Pandas DataFrame 3 min read Python | Pandas Series.apply() 3 min read Pandas dataframe.aggregate() | Python 2 min read Pandas DataFrame mean() Method 2 min read Python | Pandas Series.mean() 2 min read Python | Pandas dataframe.mad() 2 min read Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series 2 min read Python | Pandas dataframe.sem() 3 min read Python | Pandas Series.value_counts() 2 min read Pandas Index.value_counts()-Python 3 min read Applying Lambda functions to Pandas Dataframe 6 min read Manipulating DataAdding New Column to Existing DataFrame in Pandas 6 min read Python | Delete rows/columns from DataFrame using Pandas.drop() 4 min read Python | Pandas DataFrame.truncate 3 min read Python | Pandas Series.truncate() 2 min read Iterating over rows and columns in Pandas DataFrame 4 min read Pandas Dataframe.sort_values() 2 min read Python | Pandas Dataframe.sort_values() | Set-2 3 min read How to add one row in existing Pandas DataFrame? 4 min read Grouping DataPandas GroupBy 4 min read Grouping Rows in pandas 2 min read Combining Multiple Columns in Pandas groupby with Dictionary 2 min read Merging, Joining, Concatenating and ComparingPython | Pandas Merging, Joining and Concatenating 8 min read Python | Pandas Series.str.cat() to concatenate string 3 min read Python - Pandas dataframe.append() 4 min read Python | Pandas Series.append() 4 min read Pandas Index.append() - Python 2 min read Python | Pandas Series.combine() 3 min read Add a row at top in pandas DataFrame 1 min read Python | Pandas str.join() to join string/list elements with passed delimiter 2 min read Join two text columns into a single column in Pandas 2 min read How To Compare Two Dataframes with Pandas compare? 5 min read How to compare the elements of the two Pandas Series? 3 min read Working with Date and TimePython | Working with date and time using Pandas 8 min read Python | Pandas Timestamp.timestamp 3 min read Python | Pandas Timestamp.now 3 min read Python | Pandas Timestamp.isoformat 2 min read Python | Pandas Timestamp.date 2 min read Python | Pandas Timestamp.replace 3 min read Pandas.to_datetime()-Python 3 min read Python | pandas.date_range() method 4 min read Working With Text DataPython | Pandas Working With Text Data 8 min read Python | Pandas Series.str.lower(), upper() and title() 4 min read Python | Pandas Series.str.replace() to replace text in a series 5 min read Python | Pandas Series.replace() 3 min read Python | Pandas Series.str.strip(), lstrip() and rstrip() 4 min read Python | Pandas tseries.offsets.DateOffset 4 min read Like