vDataFrame[].apply

In [ ]:
vDataFrame[].apply(func: str, 
                   copy_name: str = "")

Applies a function to the vcolumn.

Parameters

Name Type Optional Description
func
str
❌
Function to use to transform the vcolumn. It must be pure SQL. The function variable must be composed of two flower brackets {}. For example to apply the function: x -> x^2 + 2 use "POWER({}, 2) + 2".
copy_name
str
✓
Name of the copy if the 'copy' parameter is set to True.

Returns

vDataFrame : self.parent

Example

In [16]:
from verticapy.datasets import load_titanic
titanic = load_titanic()
display(titanic["age"])
123
age
Numeric(6,3)
12.000
230.000
325.000
439.000
571.000
647.000
7[null]
824.000
936.000
1025.000
1145.000
1242.000
1341.000
1448.000
15[null]
1645.000
17[null]
1833.000
1928.000
2017.000
2149.000
2236.000
2346.000
24[null]
2527.000
26[null]
2747.000
2837.000
29[null]
3070.000
3139.000
3231.000
3350.000
3439.000
3536.000
36[null]
3730.000
3819.000
3964.000
40[null]
41[null]
4237.000
4347.000
4424.000
4571.000
4638.000
4746.000
48[null]
4945.000
5040.000
5155.000
5242.000
53[null]
5455.000
5542.000
56[null]
5750.000
5846.000
5950.000
6032.500
6158.000
6241.000
63[null]
64[null]
6529.000
6630.000
6730.000
6819.000
6946.000
7054.000
7128.000
7265.000
7344.000
7455.000
7547.000
7637.000
7758.000
7864.000
7965.000
8028.500
81[null]
8245.500
8323.000
8429.000
8518.000
8647.000
8738.000
8822.000
89[null]
9031.000
91[null]
9236.000
9355.000
9433.000
9561.000
9650.000
9756.000
9856.000
9924.000
100[null]
Rows: 1-100 of 1234 | Column: age | Type: numeric(6,3)
In [15]:
# the variable must be composed of two flower brackets '{}' and only pure SQL works
titanic["age"].apply(func = "POWER({}, 2)")
123
age
Float
14.0
2900.0
3625.0
41521.0
55041.0
62209.0
7[null]
8576.0
91296.0
10625.0
112025.0
121764.0
131681.0
142304.0
15[null]
162025.0
17[null]
181089.0
19784.0
20289.0
212401.0
221296.0
232116.0
24[null]
25729.0
26[null]
272209.0
281369.0
29[null]
304900.0
311521.0
32961.0
332500.0
341521.0
351296.0
36[null]
37900.0
38361.0
394096.0
40[null]
41[null]
421369.0
432209.0
44576.0
455041.0
461444.0
472116.0
48[null]
492025.0
501600.0
513025.0
521764.0
53[null]
543025.0
551764.0
56[null]
572500.0
582116.0
592500.0
601056.25
613364.0
621681.0
63[null]
64[null]
65841.0
66900.0
67900.0
68361.0
692116.0
702916.0
71784.0
724225.0
731936.0
743025.0
752209.0
761369.0
773364.0
784096.0
794225.0
80812.25
81[null]
822070.25
83529.0
84841.0
85324.0
862209.0
871444.0
88484.0
89[null]
90961.0
91[null]
921296.0
933025.0
941089.0
953721.0
962500.0
973136.0
983136.0
99576.0
100[null]
Out[15]:
Rows: 1-100 of 1234 | Column: age | Type: float
In [17]:
# creating a new vcolumn to compute the result of the transformation
titanic["age"].apply(func = "POWER({}, 2)",
                     copy_name = "age_pow_2")
123
age
Numeric(6,3)
123
age_pow_2
Float
12.0004.0
230.000900.0
325.000625.0
439.0001521.0
571.0005041.0
647.0002209.0
7[null][null]
824.000576.0
936.0001296.0
1025.000625.0
1145.0002025.0
1242.0001764.0
1341.0001681.0
1448.0002304.0
15[null][null]
1645.0002025.0
17[null][null]
1833.0001089.0
1928.000784.0
2017.000289.0
2149.0002401.0
2236.0001296.0
2346.0002116.0
24[null][null]
2527.000729.0
26[null][null]
2747.0002209.0
2837.0001369.0
29[null][null]
3070.0004900.0
3139.0001521.0
3231.000961.0
3350.0002500.0
3439.0001521.0
3536.0001296.0
36[null][null]
3730.000900.0
3819.000361.0
3964.0004096.0
40[null][null]
41[null][null]
4237.0001369.0
4347.0002209.0
4424.000576.0
4571.0005041.0
4638.0001444.0
4746.0002116.0
48[null][null]
4945.0002025.0
5040.0001600.0
5155.0003025.0
5242.0001764.0
53[null][null]
5455.0003025.0
5542.0001764.0
56[null][null]
5750.0002500.0
5846.0002116.0
5950.0002500.0
6032.5001056.25
6158.0003364.0
6241.0001681.0
63[null][null]
64[null][null]
6529.000841.0
6630.000900.0
6730.000900.0
6819.000361.0
6946.0002116.0
7054.0002916.0
7128.000784.0
7265.0004225.0
7344.0001936.0
7455.0003025.0
7547.0002209.0
7637.0001369.0
7758.0003364.0
7864.0004096.0
7965.0004225.0
8028.500812.25
81[null][null]
8245.5002070.25
8323.000529.0
8429.000841.0
8518.000324.0
8647.0002209.0
8738.0001444.0
8822.000484.0
89[null][null]
9031.000961.0
91[null][null]
9236.0001296.0
9355.0003025.0
9433.0001089.0
9561.0003721.0
9650.0002500.0
9756.0003136.0
9856.0003136.0
9924.000576.0
100[null][null]
Out[17]:
Rows: 1-100 of 1234 | Columns: 2

See Also

vDataFrame.apply Applies functions to the input vcolumns.
vDataFrame.applymap Applies a function to all the vcolumns.
vDataFrame.eval Evaluates a customized expression.