@@ -9,7 +9,7 @@ Introduction to User-Defined Functions
9
9
In pandas, User-Defined Functions (UDFs) provide a way to extend the library’s
10
10
functionality by allowing users to apply custom computations to their data. While
11
11
pandas 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
13
13
applied at different levels: element-wise, row-wise, column-wise, or group-wise,
14
14
and change the data differently, depending on the method used.
15
15
@@ -19,13 +19,13 @@ Why Use User-Defined Functions?
19
19
Pandas is designed for high-performance data processing, but sometimes your specific
20
20
needs go beyond standard aggregation, transformation, or filtering. User-defined functions allow you to:
21
21
22
- * **Customize Computations **: Implement logic tailored to your dataset, such as complex
22
+ * **Customize Computations **: Implement logic tailored to your dataset, such as complex
23
23
transformations, domain-specific calculations, or conditional modifications.
24
24
* **Improve Code Readability **: Encapsulate logic into functions rather than writing long,
25
25
complex expressions.
26
26
* **Handle Complex Grouped Operations **: Perform operations on grouped data that standard
27
27
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
29
29
are not natively available.
30
30
31
31
@@ -58,14 +58,14 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
58
58
.. ipython :: python
59
59
60
60
import pandas as pd
61
-
61
+
62
62
# Sample DataFrame
63
63
df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ]})
64
-
64
+
65
65
# User-Defined Function
66
66
def add_one (x ):
67
67
return x + 1
68
-
68
+
69
69
# Apply function
70
70
df_applied = df.apply(add_one)
71
71
print (df_applied)
@@ -81,14 +81,14 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
81
81
82
82
# Sample DataFrame
83
83
df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [1 , 2 , 3 ]})
84
-
84
+
85
85
# User-Defined Function
86
86
def add_one (x ):
87
87
return x + 1
88
88
89
89
def add_two (x ):
90
90
return x + 2
91
-
91
+
92
92
# Apply function
93
93
df_applied = df.apply({" A" : add_one, " B" : add_two})
94
94
print (df_applied)
@@ -103,11 +103,11 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
103
103
104
104
# Sample Series
105
105
s = pd.Series([1 , 2 , 3 ])
106
-
106
+
107
107
# User-Defined Function
108
108
def add_one (x ):
109
109
return x + 1
110
-
110
+
111
111
# Apply function
112
112
s_applied = s.apply(add_one)
113
113
print (s_applied)
@@ -128,11 +128,11 @@ The :meth:`DataFrame.agg` allows aggregation with a user-defined function along
128
128
' Category' : [' A' , ' A' , ' B' , ' B' ],
129
129
' Values' : [10 , 20 , 30 , 40 ]
130
130
})
131
-
131
+
132
132
# Define a function for group operations
133
133
def group_mean (group ):
134
134
return group.mean()
135
-
135
+
136
136
# Apply UDF to each group
137
137
grouped_result = df.groupby(' Category' )[' Values' ].agg(group_mean)
138
138
print (grouped_result)
@@ -149,7 +149,7 @@ transformations and custom row-wise or element-wise operations.
149
149
The :meth: `DataFrame.transform ` allows transforms a Dataframe, Series or Grouped object
150
150
while preserving the original shape of the object.
151
151
152
- .. ipython :: python
152
+ .. ipython :: python
153
153
154
154
# Sample DataFrame
155
155
df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ]})
0 commit comments