@@ -9,7 +9,7 @@ Introduction to User-Defined Functions
99In pandas, User-Defined Functions (UDFs) provide a way to extend the library’s
1010functionality by allowing users to apply custom computations to their data. While
1111pandas comes with a set of built-in functions for data manipulation, UDFs offer
12- flexibility when built-in methods are not sufficient. These functions can be
12+ flexibility when built-in methods are not sufficient. These functions can be
1313applied at different levels: element-wise, row-wise, column-wise, or group-wise,
1414and change the data differently, depending on the method used.
1515
@@ -19,13 +19,13 @@ Why Use User-Defined Functions?
1919Pandas is designed for high-performance data processing, but sometimes your specific
2020needs go beyond standard aggregation, transformation, or filtering. User-defined functions allow you to:
2121
22- * **Customize Computations **: Implement logic tailored to your dataset, such as complex
22+ * **Customize Computations **: Implement logic tailored to your dataset, such as complex
2323 transformations, domain-specific calculations, or conditional modifications.
2424* **Improve Code Readability **: Encapsulate logic into functions rather than writing long,
2525 complex expressions.
2626* **Handle Complex Grouped Operations **: Perform operations on grouped data that standard
2727 methods do not support.
28- * **Extend pandas' Functionality **: Apply external libraries or advanced calculations that
28+ * **Extend pandas' Functionality **: Apply external libraries or advanced calculations that
2929 are not natively available.
3030
3131
@@ -58,14 +58,14 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
5858.. ipython :: python
5959
6060 import pandas as pd
61-
61+
6262 # Sample DataFrame
6363 df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ]})
64-
64+
6565 # User-Defined Function
6666 def add_one (x ):
6767 return x + 1
68-
68+
6969 # Apply function
7070 df_applied = df.apply(add_one)
7171 print (df_applied)
@@ -81,14 +81,14 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
8181
8282 # Sample DataFrame
8383 df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [1 , 2 , 3 ]})
84-
84+
8585 # User-Defined Function
8686 def add_one (x ):
8787 return x + 1
8888
8989 def add_two (x ):
9090 return x + 2
91-
91+
9292 # Apply function
9393 df_applied = df.apply({" A" : add_one, " B" : add_two})
9494 print (df_applied)
@@ -103,11 +103,11 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
103103
104104 # Sample Series
105105 s = pd.Series([1 , 2 , 3 ])
106-
106+
107107 # User-Defined Function
108108 def add_one (x ):
109109 return x + 1
110-
110+
111111 # Apply function
112112 s_applied = s.apply(add_one)
113113 print (s_applied)
@@ -128,11 +128,11 @@ The :meth:`DataFrame.agg` allows aggregation with a user-defined function along
128128 ' Category' : [' A' , ' A' , ' B' , ' B' ],
129129 ' Values' : [10 , 20 , 30 , 40 ]
130130 })
131-
131+
132132 # Define a function for group operations
133133 def group_mean (group ):
134134 return group.mean()
135-
135+
136136 # Apply UDF to each group
137137 grouped_result = df.groupby(' Category' )[' Values' ].agg(group_mean)
138138 print (grouped_result)
@@ -149,7 +149,7 @@ transformations and custom row-wise or element-wise operations.
149149The :meth: `DataFrame.transform ` allows transforms a Dataframe, Series or Grouped object
150150while preserving the original shape of the object.
151151
152- .. ipython :: python
152+ .. ipython :: python
153153
154154 # Sample DataFrame
155155 df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ]})
0 commit comments