bigframes.pandas.DataFrame.map#
- DataFrame.map(func, na_action: str | None = None) DataFrame[source]#
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=Falseflag to make sure a newremote_functionis created every time we run the following code, but you can skip it to potentially reuse a previously deployedremote_functionfrom 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/Nonevalues in the data, you can ignore applying the remote function on such values by specifyingna_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_functiondecorator, 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:
- Raises:
TypeError – If value provided for
funcis not callable.ValueError – If value provided for
na_actionis notNoneorignore.