python_转换为数值类型&map映射&分桶&factorize

38 篇文章 2 订阅

python_类型转换&map映射

Transforming Data Using a Function or Mapping
# 利⽤函数或映射进⾏数据转换
# 对于许多数据集,你可能希望根据数组、Series或DataFrame列
# 中的值来实现转换⼯作。我们来看看下⾯这组有关⾁类的数据
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]})
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
meat_to_animal = {
  'bacon': 'pig',
  'pulled pork': 'pig',
  'pastrami': 'cow',
  'corned beef': 'cow',
  'honey ham': 'pig',
  'nova lox': 'salmon'
}
映射
# 使⽤Series的str.lower⽅法,将
# 各个值转换为⼩写:
lowercased = data['food'].str.lower()
lowercased
# Series的map⽅法可以接受⼀个函数或含有映射关系的字典型对  映射
# 象
data['animal'] = lowercased.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
# 使用lambda类型映射
# 使⽤map是⼀种实现元素级转换以及其他数据清理⼯作的便捷⽅
data['food'].map(lambda x: meat_to_animal[x.lower()])
# 使用lambda类型映射
# 使⽤map是⼀种实现元素级转换以及其他数据清理⼯作的便捷⽅
data['food'].map(lambda x: meat_to_animal[x.lower()])

案例二

import pandas as pd
df = pd.DataFrame([
            ['green', 'M', 10.1, 'class1'], 
            ['red', 'L', 13.5, 'class2'], 
            ['blue', 'XL', 15.3, 'class1']])
​
df.columns = ['color', 'size', 'prize', 'class label']
df
color	size	prize	class label
0	green	M	10.1	class1
1	red	L	13.5	class2
2	blue	XL	15.3	class1
size_mapping = {
           'XL': 3,
           'L': 2,
           'M': 1}
df['size'] = df['size'].map(size_mapping)
# df['size_b'] = df['size'].map(size_mapping)
df
color	size	prize	class label
0	green	1	10.1	1
1	red	2	13.5	0
2	blue	3	15.3	1
ab=enumerate(set(df['class label']))
print(ab)
<enumerate object at 0x000001F65EB16678>
# set 去除重复值
# enumerate 枚举,增加一列排序值
# 然后map映射
class_mapping = {label:idx for idx,label in enumerate(set(df['class label']))}
df['class label'] = df['class label'].map(class_mapping)
df
df
color	size	prize	class label	size_b
0	green	1	10.1	1	1
1	red	2	13.5	0	2
2	blue	3	15.3	1	3

案例三,通过pd.factorize 函数

labels, uniques = pd.factorize(['b', 'b', 'a', 'c', 'b'])
labels
array([0, 0, 1, 2, 0], dtype=int64)
uniques
array(['b', 'a', 'c'], dtype=object)
labels, uniques = pd.factorize(['b', 'b', 'a', 'c', 'b'], sort=True)
labels
array([1, 1, 0, 2, 1], dtype=int64)
>>> labels, uniques = pd.factorize(['b', 'b', 'a', 'c', 'b'], sort=True)
>>> labels
array([1, 1, 0, 2, 1])
>>> 
array(['a', 'b', 'c'], dtype=object)
uniques
array(['a', 'b', 'c'], dtype=object)

看这:
https://blog.csdn.net/dpengwang/article/details/85729480

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值