| [ Web Proxy ] |
| Viewing: https://dataframes.bigquery.dev/reference/api/bigframes.pandas.DataFrame.applymap.html | [Back] [Original] |
Section Navigation
Apply a function to a Dataframe elementwise.
This method applies a function that accepts and returns a scalar to every element of a DataFrame.
Note
In pandas 2.1.0, DataFrame.applymap is deprecated and renamed to DataFrame.map.
Examples:
Lets use reuse=False flag to make sure a new remote_function
is created every time we run the following code, but you can skip it
to potentially reuse a previously deployed remote_function from
the same user defined function.
>>> @bpd.remote_function(reuse=False, cloud_function_service_account="default")
... def minutes_to_hours(x: int) -> float:
... return x/60
>>> df_minutes = bpd.DataFrame(
... {"system_minutes" : [0, 30, 60, 90, 120],
... "user_minutes" : [0, 15, 75, 90, 6]})
>>> df_minutes
system_minutes user_minutes
0 0 0
1 30 15
2 60 75
3 90 90
4 120 6
[5 rows x 2 columns]
>>> df_hours = df_minutes.map(minutes_to_hours)
>>> df_hours
system_minutes user_minutes
0 0.0 0.0
1 0.5 0.25
2 1.0 1.25
3 1.5 1.5
4 2.0 0.1
[5 rows x 2 columns]
If there are NA/None values in the data, you can ignore
applying the remote function on such values by specifying
na_action='ignore'.
>>> df_minutes = bpd.DataFrame(
... {
... "system_minutes" : [0, 30, 60, None, 90, 120, pd.NA],
... "user_minutes" : [0, 15, 75, 90, 6, None, pd.NA]
... }, dtype="Int64")
>>> df_hours = df_minutes.map(minutes_to_hours, na_action='ignore')
>>> df_hours
system_minutes user_minutes
0 0.0 0.0
1 0.5 0.25
2 1.0 1.25
3 <NA> 1.5
4 1.5 0.1
5 2.0 <NA>
6 <NA> <NA>
[7 rows x 2 columns]
With experimental Python Transpiler enabled, you can use some lambda functions without deploying them as remote functions.
>>> bpd.options.experiments.enable_python_transpiler = True
>>> df_minutes.map(lambda hours: hours / 60)
system_minutes user_minutes
0 0.0 0.0
1 0.5 0.25
2 1.0 1.25
3 <NA> 1.5
4 1.5 0.1
5 2.0 <NA>
6 <NA> <NA>
[7 rows x 2 columns]
func (function) Python function wrapped by remote_function decorator,
returns a single value from a single value.
na_action (Optional[str], default None) {None, 'ignore'}, default None. If ignore, propagate NaN
values, without passing them to func.
Transformed DataFrame.
TypeError If value provided for func is not callable.
ValueError If value provided for na_action is not None or ignore.
Copyright 2019, Google.
Created using Sphinx 8.1.3.
Built with the PyData Sphinx Theme 0.19.0.
| Web Proxy Viewer | New URL | Original Page |