Replace column values using a mapping-logic in pandas (problem with implementing a function)(在PANDA中使用映射逻辑替换列值(实现函数的问题))
本文介绍了在PANDA中使用映射逻辑替换列值(实现函数的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下数据框。我想要的是生成另一列(freq),其中的行将根据以下逻辑具有值:
如果模式列值以数字
m开头,则在频率列中填写数字n。- m: 1, n: 12 - m: 6, n: 4 - m: 7, n: 2 - m: 8, n: 1
DataFrame
Mode
0 602
1 603
2 700
3 100
4 100
5 100
6 802
7 100
8 100
9 100
10 100
以下是我尝试实现的逻辑。但不知何故,它似乎并没有奏效。即使您可以建议一些替代解决方案,而不使用我的代码,也同样有效。
def check_mode(Mode):
freq = ''
if (Mode.str.startswith('8')).any():
freq = 1
elif (Mode.startswith("7")).all():
freq = 2
elif (Mode.startswith("6")).any():
freq = 4
elif (Mode.startswith("1")).any():
freq = 12
return freq
df['freq']=check_mode(df_ia['Mode'].values)
如果我使用:
if (Mode.str.startswith('8')).any():
我收到错误:
AttributeError: 'numpy.ndarray' object has no attribute 'str'
如果我使用:
if (Mode.startswith('8')).any():
我收到:
AttributeError: 'numpy.ndarray' object has no attribute 'startswith'
任何帮助都将不胜感激。谢谢。
推荐答案
试试这个。一个线条。
df['freq'] = df.Mode.astype(str).str.get(0).replace({'8': 1, '7': 2, '6': 4, '1': 12})
现在让我们解开它的用途:
# You can run this cell and check the result as well
(df.Mode.astype(str) # convert the column "Mode" into str data type
.str.get(0) # get string based methods and access the get
# method to get the 1st (`.get(0)`) digit
# replace the digits with a dictionary that
# maps to their replacement values.
.replace({'8': 1, '7': 2, '6': 4, '1': 12}))
df = pd.DataFrame([602, 603, 700, 100, 100, 100, 802, 100, 100, 100, 100,], columns=['Mode'])
df['freq'] = df.Mode.astype(str).str.get(0).replace({'8': 1, '7': 2, '6': 4, '1': 12})
df
## Output
# Mode freq
# 0 602 4
# 1 603 4
# 2 700 2
# 3 100 12
# 4 100 12
# 5 100 12
# 6 802 1
# 7 100 12
# 8 100 12
# 9 100 12
# 10 100 12
这篇关于在PANDA中使用映射逻辑替换列值(实现函数的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:在PANDA中使用映射逻辑替换列值(实现函数的问题)
基础教程推荐
猜你喜欢
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
