英文:
How to convert an object that is a fraction to an integer in Python?
问题
> ValueError: 无效的int()字面值,使用基数10: '100/100'
我的数据集有一个评分列,其中保存了一个以对象形式呈现的评分,例如:98/100、99/100、60/100等。
我需要将这一列转换为整数,如:98、99、60等。
我尝试过使用.astype
和int(float())
。
英文:
> ValueError: invalid literal for int() with base 10: '100/100'
My data set has a rating column that holds a rating in the form of an object presented as: 98/100, 99/100, 60/100, etc.
I need to convert this column to an int such as: 98, 99, 60, etc.
I tried .astype
and int(float())
.
答案1
得分: 2
以下是代码部分的翻译:
df = df['rating'].apply(lambda x: x.split('/')[0]).astype(int))
import pandas as pd
df = pd.DataFrame.from_dict({'rating': ['90/100', '88/100', '35/100']})
df['rating'] = df['rating'].apply(lambda x: x.split('/')[0]).astype(int)
print(df)
rating
0 90
1 88
2 35
df['rating'] = df['rating'].apply(lambda x: (int(x.split('/')[0]) / int(x.split('/')[1])) * 100).astype(int)
df['rating'] = df['rating'].apply(lambda x: eval(x) * 100).astype(int)
希望这些翻译对你有帮助。如果有其他问题,请随时提出。
英文:
Taking into account that:
- You are talking about pandas dataframes
- Each grade is in the form
X/100
, if not - then a basic computation can be done in thelambda
function below.
you can use the apply
method and then use the astype
to convert the column to integer values:
df = df['rating'].apply(lambda x: x.split('/')[0]).astype(int))
import pandas as pd
df = pd.DataFrame.from_dict({'rating': ['90/100', '88/100', '35/100']})
df['rating'] = df['rating'].apply(lambda x: x.split('/')[0]).astype(int)
print(df)
Returns:
rating
0 90
1 88
2 35
The more generic computation if the grade is in the format X/Y
:
df['rating'] = df['rating'].apply(lambda x: (int(x.split('/')[0]) / int(x.split('/')[1])) * 100).astype(int)
Or shortly, but extremely unsafe and not a good practice at all:
df['rating'] = df['rating'].apply(lambda x: eval(x) * 100).astype(int)
You can use it if you're just "fiddling around" with Python
答案2
得分: 2
'98/100'
是表示分数的字符串。
@no_hex 的回答展示了如何使用字符串操作解析分子。不过Python也支持分数,所以使用它们可能会更容易。
from fractions import Fraction
s = "98/100"
int(Fraction(s) * 100)
98
作为一个附带好处,如果分数的分母不同,这也将给出正确的答案。
s = "3/4"
float(Fraction(s) * 100)
75.0
英文:
'98/100'
is a string representing a fraction.
@no_hex's answer shows a way to parse the numerator using string operations.
However Python has also support for fractions, so it might be easier to use those.
>> from fractions import Fraction
>> s = "98/100"
>> int(Fraction(s) * 100)
98
As a side benefit, this will also give the correct answer if the fraction denominator is different.
>> s = "3/4"
>> float(Fraction(s) * 100)
75.0
答案3
得分: 0
int(s.split('/')[0])
英文:
s = '60/100'
int(s.split('/')[0])
答案4
得分: -1
你需要将其四舍五入到最接近的整数值,然后可以将其转换为整数,例如,3/5会得到0.6,但如果你尝试将其转换为整数,它不会起作用,因为它是一个小数值,但如果你四舍五入它,你可以将其转换为整数。
还取决于你想要做什么。
英文:
You will have to round it up to the nearest interger value and then you can cast it to a interger so for example 3/5 would be 0.6 but if you try cast that, it won't work since its a decimal value but if you round it up you can cast it to a interger
Also depends on what you'd like to do
答案5
得分: -2
尝试这段代码:
result = int(valueOfyoucolumn) * 100
通过这样做,您将移除小数部分并将结果转换为整数。
英文:
Try this code:
result = int(valueOfyoucolumn) * 100
By this, you are removing the fraction and you convert the results into an interger.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论