bigframes.pandas.DataFrame.applymap#

DataFrame.applymap(func, na_action: str | None = None) DataFrame#

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:

Let’s 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]
Parameters:
  • 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.

Returns:

Transformed DataFrame.

Return type:

bigframes.pandas.DataFrame

Raises:
  • TypeError – If value provided for func is not callable.

  • ValueError – If value provided for na_action is not None or ignore.