Map

2019/11/06

map

python 语法中的map

map(function, iterable, …)

  • function: 函数
  • iterable: 序列(一个或多个)
import pandas as pd

计算列表各个元素的平方

def square(x):
    return x ** 2

_list = [1, 2, 3, 4, 5]
_map = map(square, _list) 
# python3的map()函数返回的是迭代器,可以通过遍历迭代器查看值
for i in _map:
    print(i)
1
4
9
16
25
# 使用匿名函数
map(lambda x: x ** 2, [1, 2, 3, 4, 5]) 
<map at 0x110d40630>

pandas 中的map

Series

Series中的map方法可以接受一个函数或字典型对象

字典映射

data = pd.DataFrame({'food':['bacon','pulled pork','bacon','Pastrami',
                             'corned beef','Bacon','pastrami','honey ham','nova lox'],
                     'ounces':[4,3,12,6,7.5,8,3,5,6]})
meat_to_animal = {
 'bacon':'pig',
 'pulled pork':'pig',
 'pastrami':'cow',
 'corned beef':'cow',
 'honey ham':'pig',
 'nova lox':'salmon' } 
data
food ounces
0 bacon 4.0
1 pulled pork 3.0
2 bacon 12.0
3 Pastrami 6.0
4 corned beef 7.5
5 Bacon 8.0
6 pastrami 3.0
7 honey ham 5.0
8 nova lox 6.0
data['animal'] = data['food'].map(str.lower).map(meat_to_animal) 
data
food ounces animal
0 bacon 4.0 pig
1 pulled pork 3.0 pig
2 bacon 12.0 pig
3 Pastrami 6.0 cow
4 corned beef 7.5 cow
5 Bacon 8.0 pig
6 pastrami 3.0 cow
7 honey ham 5.0 pig
8 nova lox 6.0 salmon

函数

index = pd.date_range('2019-11-06', periods=10)
index
DatetimeIndex(['2019-11-06', '2019-11-07', '2019-11-08', '2019-11-09',
               '2019-11-10', '2019-11-11', '2019-11-12', '2019-11-13',
               '2019-11-14', '2019-11-15'],
              dtype='datetime64[ns]', freq='D')
ser = pd.Series(list(range(10)), index=index)
ser
2019-11-06    0
2019-11-07    1
2019-11-08    2
2019-11-09    3
2019-11-10    4
2019-11-11    5
2019-11-12    6
2019-11-13    7
2019-11-14    8
2019-11-15    9
Freq: D, dtype: int64
ser.index.map(lambda x: x.day)
Int64Index([6, 7, 8, 9, 10, 11, 12, 13, 14, 15], dtype='int64')
ser.index.map(lambda x: x.weekday) # 0(星期一)到6(星期日)
Int64Index([2, 3, 4, 5, 6, 0, 1, 2, 3, 4], dtype='int64')
ser.map(lambda x: x+10)
2019-11-06    10
2019-11-07    11
2019-11-08    12
2019-11-09    13
2019-11-10    14
2019-11-11    15
2019-11-12    16
2019-11-13    17
2019-11-14    18
2019-11-15    19
Freq: D, dtype: int64
def f(x):
    if x < 5: 
        return True
    else:
        return False
ser.map(f)
2019-11-06     True
2019-11-07     True
2019-11-08     True
2019-11-09     True
2019-11-10     True
2019-11-11    False
2019-11-12    False
2019-11-13    False
2019-11-14    False
2019-11-15    False
Freq: D, dtype: bool

DataFrame的applymap()函数

对DataFrame里的每个值进行处理,然后返回一个新的DataFrame

df = pd.DataFrame({
    'a': [1, 2, 3],
    'b': [10, 20, 30],
    'c': [5, 15, 25]
})
df
a b c
0 1 10 5
1 2 20 15
2 3 30 25
df.applymap(lambda x: x+1)
a b c
0 2 11 6
1 3 21 16
2 4 31 26

示例

有一组数据是10个学生的两次考试成绩,要求把成绩转换成ABCD等级:

转换规则是:

  • 90-100 -> A
  • 80-89 -> B
  • 70-79 -> C
  • 60-69 -> D
  • 0-59 -> F
grades_df = pd.DataFrame(
    data={'exam1': [43, 81, 78, 75, 89, 70, 91, 65, 98, 87],
          'exam2': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]},
    index=['Andre', 'Barry', 'Chris', 'Dan', 'Emilio', 
           'Fred', 'Greta', 'Humbert', 'Ivan', 'James']
)
grades_df
exam1 exam2
Andre 43 24
Barry 81 63
Chris 78 56
Dan 75 56
Emilio 89 67
Fred 70 51
Greta 91 79
Humbert 65 46
Ivan 98 72
James 87 60
def convert_to_letter(score):
    if (score >= 90):
        return 'A'
    elif (score >= 80):
        return 'B'
    elif (score >= 70):
        return 'C'
    elif (score >= 60):
        return 'D'
    else:
        return 'F'
    
def convert_grades(grades):
    return grades.applymap(convert_to_letter)

convert_grades(grades_df)
exam1 exam2
Andre F F
Barry B D
Chris C F
Dan C F
Emilio B D
Fred C F
Greta A C
Humbert D F
Ivan A C
James B D

Search

    Post Directory