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
相关文章
相关标签/搜索