Series.apply(func, convert_dtype=True, args=(), **kwd)[source]
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)
沿著DataFrame的軸應用一個函數
傳遞給函數的對象是Series對象,其索引要麼是DataFrame的索引(軸=0),要麼是DataFrame的列(axis=1)。默認情況下(result_type=None),最終的返回類型是從應用函數的返回類型推斷出來的。否則,它取決於result_type參數
參數:
funcfunction
Function to apply to each column or row.
axis{0 or 『index』, 1 or 『columns』}, default 0
函數所應用的軸:
0 或 『index』: 對每一列應用函數
1 或 『columns』: 對每一行應用函數
rawbool, default False
Determines if row or column is passed as a Series or ndarray object:
result_type{『expand』, 『reduce』, 『broadcast』, None}, default None
這些只在axis=1(列)時起作用:
默認行為(None)取決於應用函數的返回值:類似列表的結果將作為這些結果的Series返回。但是,如果apply函數返回一個Series,這些列就會展開為列.
argstuple
除了array/series外,還要傳遞給func的位置參數。
**kwds
要作為關鍵字參數傳遞給func的其他關鍵字參數。
返回:
>>> s = pd.Series([20, 21, 12],... index=['London', 'New York', 'Helsinki'])>>> sLondon 20New York 21Helsinki 12dtype: int64>>> s.apply(lambda x: x ** 2)London 400New York 441Helsinki 144dtype: int64>>> def subtract_custom_value(x, custom_value):... return x - custom_value>>> s.apply(subtract_custom_value, args=(5,))London 15New York 16Helsinki 7dtype: int64
>>> df = pd.DataFrame([[4, 9],] * 3, columns=['A', 'B'])>>> df A B0 4 91 4 92 4 9>>> df.apply(np.sqrt) A B0 2.0 3.01 2.0 3.02 2.0 3.0>>> df.apply(np.sum, axis=0)A 12B 27dtype: int64>>> df.apply(np.sum, axis=1)0 131 132 13dtype: int64>>> df.apply(lambda x: [1, 2], axis=1)0 [1, 2]1 [1, 2]2 [1, 2]dtype: object>>> df.apply(lambda x: [1, 2], axis=1, result_type='expand') 0 10 1 21 1 22 1 2>>> df.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1) foo bar0 1 21 1 22 1 2>>> df.apply(lambda x: [1, 2], axis=1, result_type='broadcast') A B0 1 21 1 22 1 2Series.map(arg, na_action=None)[source]
map方法都是把對應的數據逐個當作參數傳入到字典或函數中,得到映射後的值
>>> s = pd.Series(['cat', 'dog', np.nan, 'rabbit'])>>> s0 cat1 dog2 NaN3 rabbitdtype: object>>> s.map({'cat': 'kitten', 'dog': 'puppy'})0 kitten1 puppy2 NaN3 NaNdtype: object>>> s.map('I am a {}'.format)0 I am a cat1 I am a dog2 I am a nan3 I am a rabbitdtype: object
>>> s.map('I am a {}'.format, na_action='ignore')0 I am a cat1 I am a dog2 NaN3 I am a rabbitdtype: objectSeries.transform(func, axis=0, *args, **kwargs)[source]
Call func on self producing a Series with transformed values.
Produced Series will have same axis length as self.
funcfunction, str, list-like or dict-like
Function to use for transforming the data. If a function, must either work when passed a Series or when passed to Series.apply. If func is both list-like and dict-like, dict-like behavior takes precedence.
Accepted combinations are:
function
string function name
list-like of functions and/or function names, e.g. [np.exp, 'sqrt']
dict-like of axis labels -> functions, function names or list-like of such.
axis{0 or 『index』}
Parameter needed for compatibility with DataFrame.
*args
Positional arguments to pass to func.
**kwargs
Keyword arguments to pass to func
>>> df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})>>> df A B0 0 11 1 22 2 3>>> df.transform(lambda x: x + 1) A B0 1 21 2 32 3 4>>> s = pd.Series(range(3))>>> s0 01 12 2dtype: int64>>> s.transform([np.sqrt, np.exp]) sqrt exp0 0.000000 1.0000001 1.000000 2.7182822 1.414214 7.389056>>> df = pd.DataFrame({... "c": [1, 1, 1, 2, 2, 2, 2],... "type": ["m", "n", "o", "m", "m", "n", "n"]... })>>> df c type0 1 m1 1 n2 1 o3 2 m4 2 m5 2 n6 2 n
>>> df['size'] = df.groupby('c')['type'].transform(len)>>> df c type size0 1 m 31 1 n 32 1 o 33 2 m 44 2 m 45 2 n 46 2 n 4DataFrame.applymap(func, na_action=None)[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.
unccallable
Python function, returns a single value from a single value.
na_action{None, 『ignore』}, default None
If 『ignore』, propagate NaN values, without passing them to func.
New in version 1.2.
DataFrame
Transformed DataFrame.
>>> df = pd.DataFrame([[1, 2.12], [3.356, 4.567]])>>> df 0 10 1.000 2.1201 3.356 4.567>>> df.applymap(lambda x: len(str(x))) 0 10 3 41 5 5