Difference between map, applymap and apply methods in Pandas

前端 未结 10 1994
刺人心
刺人心 2020-11-22 03:00

Can you tell me when to use these vectorization methods with basic examples?

I see that map is a Series method whereas the rest are

10条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 03:13

    Adding to the other answers, in a Series there are also map and apply.

    Apply can make a DataFrame out of a series; however, map will just put a series in every cell of another series, which is probably not what you want.

    In [40]: p=pd.Series([1,2,3])
    In [41]: p
    Out[31]:
    0    1
    1    2
    2    3
    dtype: int64
    
    In [42]: p.apply(lambda x: pd.Series([x, x]))
    Out[42]: 
       0  1
    0  1  1
    1  2  2
    2  3  3
    
    In [43]: p.map(lambda x: pd.Series([x, x]))
    Out[43]: 
    0    0    1
    1    1
    dtype: int64
    1    0    2
    1    2
    dtype: int64
    2    0    3
    1    3
    dtype: int64
    dtype: object
    

    Also if I had a function with side effects, such as "connect to a web server", I'd probably use apply just for the sake of clarity.

    series.apply(download_file_for_every_element) 
    

    Map can use not only a function, but also a dictionary or another series. Let's say you want to manipulate permutations.

    Take

    1 2 3 4 5
    2 1 4 5 3
    

    The square of this permutation is

    1 2 3 4 5
    1 2 5 3 4
    

    You can compute it using map. Not sure if self-application is documented, but it works in 0.15.1.

    In [39]: p=pd.Series([1,0,3,4,2])
    
    In [40]: p.map(p)
    Out[40]: 
    0    0
    1    1
    2    4
    3    2
    4    3
    dtype: int64
    

提交回复
热议问题