简体   繁体   English

无法将浮点数转换为整数

[英]Unable to convert floats to ints

I have a dataset about movies and there's a column that corresponds to each movies' release year. 我有一个关于电影的数据集,有一个对应于每个电影发行年份的列。 The numbers are currently all floats and I'm trying to convert them to integer. 这些数字目前都是浮点数,我正在尝试将其转换为整数。

This is the code I've written to do so: 这是我编写的代码:

if isinstance (data['Year'], float):
    for idx, i in enumerate(data['Year']):
        data['Year'][idx]= int(i)
else:
    pass

The code runs. 代码运行。 However, whenever i try to print the data type of each element in the column (like: 但是,每当我尝试打印列中每个元素的数据类型时(例如:

for i in data['Year]:
print(type(i))

the results I get is still all floats. 我得到的结果仍然是浮点数。

Anyone has any idea of why this wouldn't be working? 任何人都知道为什么这行不通吗? Help would be very much appreciated it! 帮助将不胜感激!

I think data['Year'] is a list instead of float in your code. 我认为data['Year']是一个list而不是代码中的float Try 尝试

data = {'Year': [1990.0, 1991, 2000.0]}

for idx, i in enumerate(data['Year']):
    if isinstance(i, float):
        data['Year'][idx]= int(i)

for i in data['Year']:
    print(type(i))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM