怎么用python算单价和总价成什么比例_python – 从两列计算和创建百分比列

该篇博客介绍如何在Python中将含有数字的字符串列转换为浮点数或整数,然后计算并创建新的百分比列来表示单价在总价中所占的比例。通过示例代码展示了使用pandas DataFrame进行数据类型转换和计算过程。
摘要由CSDN通过智能技术生成

我认为你需要将字符串列转换为float或int,因为它们的类型是字符串(但看起来像数字):

Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(float)

Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(float)

Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(int)

Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(int)

样品:

import pandas as pd

Good_apples = ["10", "20", "3", "7", "9"]

Total_apples = ["20", "80", "30", "70", "90"]

d = {"Good_apples": Good_apples, "Total_apples": Total_apples}

Apple_farm = pd.DataFrame(d)

print Apple_farm

Good_apples Total_apples

0 10 20

1 20 80

2 3 30

3 7 70

4 9 90

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值