python - 如何根据 ID 号列表乘以列值?
问题描述
我有一个数据集,想根据列表的 ID 值将值列乘以 2.5。
我的数据框看起来像这样
Name ID Salary
James 21 25,000
Sam 12 15,000
我的列表是一个系列,我们称它为 s = ["21", "36"] 这个数据是 ID 号
如何根据身份证号码将工资乘以2.5?
目标是拥有这样的东西
Name ID Salary
James 21 62,500
Sam 12 15,000
解决方案
首先转换Salary
为数字,然后将值转换ID
为字符串,test bySeries.isin
和 multiple byDataFrame.loc
以通过掩码选择行和按名称选择列Salary
:
s = ["21", "36"]
#if values of Salary are strings
#df = pd.read_csv(file, thousands=',')
#or
#df['Salary'] = df['Salary'].str.replace(',','').astype(int)
#ID are converted to strings by `astype`, because valus in list s are strings
df.loc[df['ID'].astype(str).isin(s), 'Salary'] *= 2.5
#if s are numeric
#df.loc[df['ID'].isin(s), 'Salary'] *= 2.5
print (df)
Name ID Salary
0 James 21 62500.0
1 Sam 12 15000.0
推荐阅读
- java - 尝试显示我的应用的天气图标时遇到问题
- cpu - cpu 的 sys.dm_os_ring_buffers 与 wmi 查询
- node.js - 如何在 Elasticsearch 中建立 N-Gram 关系
- netsuite - 工作订单状态更改 - 由于“特殊订单项目”而创建工作订单时触发脚本
- javascript - html/css 中的对齐修复
- css - 使用 Chakra-UI 抽屉组件推送内容
- java - 如何在kafka消费者中按内容设置主题?
- dji-sdk - 渲染问题 - Android Studio 没有渲染布局
- python - 试图查看 python 版本返回 -bash: /usr/local/bin/python3.7: No such file or directory
- java - 如何在多pod环境中只运行一次spring boot启动方法