MCPcopy
hub / github.com/pandas-dev/pandas / map

Method map

pandas/core/frame.py:12496–12586  ·  view source on GitHub ↗

Apply a function to a Dataframe elementwise. .. versionadded:: 2.1.0 DataFrame.applymap was deprecated and renamed to DataFrame.map. This method applies a function that accepts and returns a scalar to every element of a DataFrame. Parameters

(
        self, func: PythonFuncType, na_action: Literal["ignore"] | None = None, **kwargs
    )

Source from the content-addressed store, hash-verified

12494 raise ValueError(f"Unknown engine {engine}")
12495
12496 def map(
12497 self, func: PythonFuncType, na_action: Literal["ignore"] | None = None, **kwargs
12498 ) -> DataFrame:
12499 """
12500 Apply a function to a Dataframe elementwise.
12501
12502 .. versionadded:: 2.1.0
12503
12504 DataFrame.applymap was deprecated and renamed to DataFrame.map.
12505
12506 This method applies a function that accepts and returns a scalar
12507 to every element of a DataFrame.
12508
12509 Parameters
12510 ----------
12511 func : callable
12512 Python function, returns a single value from a single value.
12513 na_action : {None, 'ignore'}, default None
12514 If 'ignore', propagate NaN values, without passing them to func.
12515 **kwargs
12516 Additional keyword arguments to pass as keywords arguments to
12517 `func`.
12518
12519 Returns
12520 -------
12521 DataFrame
12522 Transformed DataFrame.
12523
12524 See Also
12525 --------
12526 DataFrame.apply : Apply a function along input axis of DataFrame.
12527 DataFrame.replace: Replace values given in `to_replace` with `value`.
12528 Series.map : Apply a function elementwise on a Series.
12529
12530 Examples
12531 --------
12532 >>> df = pd.DataFrame([[1, 2.12], [3.356, 4.567]])
12533 >>> df
12534 0 1
12535 0 1.000 2.120
12536 1 3.356 4.567
12537
12538 >>> df.map(lambda x: len(str(x)))
12539 0 1
12540 0 3 4
12541 1 5 5
12542
12543 Like Series.map, NA values can be ignored:
12544
12545 >>> df_copy = df.copy()
12546 >>> df_copy.iloc[0, 0] = pd.NA
12547 >>> df_copy.map(lambda x: len(str(x)), na_action="ignore")
12548 0 1
12549 0 NaN 4
12550 1 5.0 5
12551
12552 It is also possible to use `map` with functions that are not
12553 `lambda` functions:

Callers 12

setupMethod · 0.95
test_map_keeps_dtypeFunction · 0.95
test_map_strFunction · 0.95
test_map_datetimelikeFunction · 0.95
test_map_boxFunction · 0.95
test_map_typeFunction · 0.95
test_datetime_timeFunction · 0.95

Calls 3

applyMethod · 0.95
__finalize__Method · 0.80
copyMethod · 0.45

Tested by 11

test_map_keeps_dtypeFunction · 0.76
test_map_strFunction · 0.76
test_map_datetimelikeFunction · 0.76
test_map_boxFunction · 0.76
test_map_typeFunction · 0.76
test_datetime_timeFunction · 0.76