@@ -13,10 +13,6 @@ 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
depending on the method used.
15
15
16
- .. .. note::
17
-
18
- .. User-Defined Functions will be abbreviated to UDFs throughout this guide.
19
-
20
16
Why Use User-Defined Functions?
21
17
-------------------------------
22
18
@@ -36,7 +32,7 @@ needs go beyond standard aggregation, transformation, or filtering. UDFs allow y
36
32
What functions support User-Defined Functions
37
33
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38
34
39
- UDFs can be applied across various pandas methods that work with Series and DataFrames:
35
+ User-Defined Functions can be applied across various pandas methods that work with Series and DataFrames:
40
36
41
37
* :meth: `DataFrame.apply ` - A flexible method that allows applying a function to Series,
42
38
DataFrames, or groups of data.
@@ -60,7 +56,6 @@ ways to apply user-defined functions across different pandas data structures.
60
56
The :meth: `DataFrame.apply ` allows applying a user-defined functions along either axis (rows or columns):
61
57
62
58
.. ipython :: python
63
-
64
59
import pandas as pd
65
60
66
61
# Sample DataFrame
@@ -71,8 +66,8 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
71
66
return x + 1
72
67
73
68
# Apply function
74
- df_transformed = df.apply(add_one)
75
- print (df_transformed )
69
+ df_applied = df.apply(add_one)
70
+ print (df_applied )
76
71
77
72
# This works with lambda functions too
78
73
df_lambda = df.apply(lambda x : x + 1 )
@@ -82,9 +77,6 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
82
77
:meth: `DataFrame.apply ` also accepts dictionaries of multiple user-defined functions:
83
78
84
79
.. ipython :: python
85
-
86
- import pandas as pd
87
-
88
80
# Sample DataFrame
89
81
df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [1 , 2 , 3 ]})
90
82
@@ -96,8 +88,8 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
96
88
return x + 2
97
89
98
90
# Apply function
99
- df_transformed = df.apply({" A" : add_one, " B" : add_two})
100
- print (df_transformed )
91
+ df_applied = df.apply({" A" : add_one, " B" : add_two})
92
+ print (df_applied )
101
93
102
94
# This works with lambda functions too
103
95
df_lambda = df.apply({" A" : lambda x : x + 1 , " B" : lambda x : x + 2 })
@@ -106,9 +98,6 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
106
98
:meth: `DataFrame.apply ` works with Series objects as well:
107
99
108
100
.. ipython :: python
109
-
110
- import pandas as pd
111
-
112
101
# Sample Series
113
102
s = pd.Series([1 , 2 , 3 ])
114
103
@@ -117,8 +106,8 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
117
106
return x + 1
118
107
119
108
# Apply function
120
- s_transformed = s.apply(add_one)
121
- print (df_transformed )
109
+ s_applied = s.apply(add_one)
110
+ print (s_applied )
122
111
123
112
# This works with lambda functions too
124
113
s_lambda = s.apply(lambda x : x + 1 )
@@ -127,10 +116,9 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
127
116
:meth: `DataFrame.agg `
128
117
---------------------
129
118
130
- When working with grouped data, user-defined functions can be used within :meth: ` DataFrame.agg ` :
119
+ The :meth: ` DataFrame.agg ` allows aggregation with a user-defined function along either axis (rows or columns) :
131
120
132
121
.. ipython :: python
133
-
134
122
# Sample DataFrame
135
123
df = pd.DataFrame({
136
124
' Category' : [' A' , ' A' , ' B' , ' B' ],
@@ -145,6 +133,78 @@ When working with grouped data, user-defined functions can be used within :meth:
145
133
grouped_result = df.groupby(' Category' )[' Values' ].agg(group_mean)
146
134
print (grouped_result)
147
135
136
+ In terms of the API, :meth: `DataFrame.agg ` has similar usage to :meth: `DataFrame.apply `,
137
+ but it is primarily used for **aggregation **, applying functions that summarize or reduce data.
138
+ Typically, the result of :meth: `DataFrame.agg ` reduces the dimensions of data as shown
139
+ in the above example. Conversely, :meth: `DataFrame.apply ` is more general and allows for both
140
+ transformations and custom row-wise or element-wise operations.
141
+
142
+ :meth: `DataFrame.transform `
143
+ ---------------------------
144
+
145
+ The :meth: `DataFrame.transform ` allows transforms a Dataframe, Series or Grouped object
146
+ while preserving the original shape of the object.
147
+
148
+ .. ipython :: python
149
+ # Sample DataFrame
150
+ df = pd.DataFrame({' A' : [1 , 2 , 3 ], ' B' : [4 , 5 , 6 ]})
151
+
152
+ # User-Defined Function
153
+ def double (x ):
154
+ return x * 2
155
+
156
+ # Apply transform
157
+ df_transformed = df.transform(double)
158
+ print (df_transformed)
159
+
160
+ # This works with lambda functions too
161
+ df_lambda = df.transform(lambda x : x * 2 )
162
+ print (df_lambda)
163
+
164
+ Attempting to use common aggregation functions such as `mean ` or `sum ` will result in
165
+ values being broadcasted to the original dimensions:
166
+
167
+ .. ipython :: python
168
+ # Sample DataFrame
169
+ df = pd.DataFrame({
170
+ ' Category' : [' A' , ' A' , ' B' , ' B' , ' B' ],
171
+ ' Values' : [10 , 20 , 30 , 40 , 50 ]
172
+ })
173
+
174
+ # Using transform with mean
175
+ df[' Mean_Transformed' ] = df.groupby(' Category' )[' Values' ].transform(' mean' )
176
+
177
+ # Using transform with sum
178
+ df[' Sum_Transformed' ] = df.groupby(' Category' )[' Values' ].transform(' sum' )
179
+
180
+ # Result broadcasted to DataFrame
181
+ print (df)
182
+
183
+ :meth: `DataFrame.filter `
184
+ ------------------------
185
+
186
+ The :meth: `DataFrame.filter ` method is used to select subsets of the DataFrame’s
187
+ columns or rows and accepts user-defined functions. Specifically, these functions
188
+ return boolean values to filter columns or rows. It is useful when you want to
189
+ extract specific columns or rows that match particular conditions.
190
+
191
+ .. ipython :: python
192
+ # Sample DataFrame
193
+ df = pd.DataFrame({
194
+ ' A' : [1 , 2 , 3 ],
195
+ ' B' : [4 , 5 , 6 ],
196
+ ' C' : [7 , 8 , 9 ],
197
+ ' D' : [10 , 11 , 12 ]
198
+ })
199
+
200
+ # Define a function that filters out columns where the name is longer than 1 character
201
+ df_filtered_func = df.filter(items = lambda x : len (x) > 1 )
202
+ print (df_filtered_func)
203
+
204
+ Unlike the methods discussed earlier, :meth: `DataFrame.filter ` does not accept
205
+ functions that do not return boolean values, such as `mean ` or `sum `.
206
+
207
+
148
208
Performance Considerations
149
209
--------------------------
150
210
0 commit comments