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


有時候需要對 pandas Series 里的值進行一些操作,但是沒有內置函數,這時候可以自己寫一個函數,使用 pandas Series 的 apply 方法,可以對里面的每個值都調用這個函數,然后返回一個新的 Series 

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"

可以使用apply方法:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM