pandas數組(pandas Series)-(5)apply方法自定義函數

有時候須要對 pandas Series 裏的值進行一些操做,可是沒有內置函數,這時候能夠本身寫一個函數,使用 pandas Series 的 apply 方法,能夠對裏面的每一個值都調用這個函數,而後返回一個新的 Series app

import pandas as pd

s = pd.Series([1, 2, 3, 4, 5])
def add_one(x):
    return x + 1

print s.apply(add_one)

# 結果:
0    2
1    3
2    4
3    5
4    6
dtype: int64

 

一個栗子:函數

names = pd.Series([
    'Andre Agassi',
    'Barry Bonds',
    'Christopher Columbus',
    'Daniel Defoe',
    'Emilio Estevez',
    'Fred Flintstone',
    'Greta Garbo',
    'Humbert Humbert',
    'Ivan Ilych',
    'James Joyce',
    'Keira Knightley',
    'Lois Lane',
    'Mike Myers',
    'Nick Nolte',
    'Ozzy Osbourne',
    'Pablo Picasso',
    'Quirinus Quirrell',
    'Rachael Ray',
    'Susan Sarandon',
    'Tina Turner',
    'Ugueth Urbina',
    'Vince Vaughn',
    'Woodrow Wilson',
    'Yoji Yamada',
    'Zinedine Zidane'
])

把以上Series裏的名字從"Firstname Lastname" 轉換成 "Lastname, FirstName"ui

可使用apply方法:spa

def reverse_name(name):
    name_array = name.split(' ')
    
    new_name = '{}, {}'.format(name_array[1],name_array[0])
    return new_name

print(names.apply(reverse_name))
0             Agassi, Andre
1              Bonds, Barry
2     Columbus, Christopher
3             Defoe, Daniel
4           Estevez, Emilio
5          Flintstone, Fred
6              Garbo, Greta
7          Humbert, Humbert
8               Ilych, Ivan
9              Joyce, James
10         Knightley, Keira
11               Lane, Lois
12              Myers, Mike
13              Nolte, Nick
14           Osbourne, Ozzy
15           Picasso, Pablo
16       Quirrell, Quirinus
17             Ray, Rachael
18          Sarandon, Susan
19             Turner, Tina
20           Urbina, Ugueth
21            Vaughn, Vince
22          Wilson, Woodrow
23             Yamada, Yoji
24         Zidane, Zinedine
dtype: object
相關文章
相關標籤/搜索