Python 3.x Python:尝试将单列从Float转换为Int

Python 3.x Python:尝试将单列从Float转换为Int,python-3.x,pandas,Python 3.x,Pandas,Python新手。在pythonywhere中使用大型数据集。出于某种原因,我的CSV将“年”作为文本输入。我可以用pd.to_numeric把它变成一个数字。但现在它是一个浮点数,我想要一个int。我尝试了.dropna().apply(np.int64),但它仍然以int的形式出现。我需要dropna,因为显然缺少一些值 代码: Out:Name:Year,dtype:float64我不确定您的示例数据是什么样子,但请尝试使用 “.astype(int)”代替“.apply(np.int64

Python新手。在pythonywhere中使用大型数据集。出于某种原因,我的CSV将“年”作为文本输入。我可以用pd.to_numeric把它变成一个数字。但现在它是一个浮点数,我想要一个int。我尝试了.dropna().apply(np.int64),但它仍然以int的形式出现。我需要dropna,因为显然缺少一些值 代码:


Out:Name:Year,dtype:float64

我不确定您的示例数据是什么样子,但请尝试使用

“.astype(int)”代替“.apply(np.int64)”

这是因为pandas dataframe对象由numpy数组组成。因此,此numpy转换应该适用于您的操作


可以找到有关此操作的文档。

此规则对
numpy
pandas
均有效。 每当转换
numpy
中的数组或
pandas
中的列的数据类型时,如果
c
是您的数组或列(
pandas.Series
),则
c.astype(dtype)
将其转换为数据类型:dtype

示例

c.astype(bool)
c.astype(np.int64)
c.astype(float)
c.astype(int)
c.astype(np.float32)

我很困惑。根据您给定的输入,您的代码适用于我:

import pandas as pd, numpy as np
from io import StringIO

input = """
movieId,title,Year
1,Toy Story (1995),1995.0 
2,Jumanji (1995),1995.0
"""

df = pd.read_csv(StringIO(input))
df['Year'] = df['Year'].dropna().apply(np.int64)
print(df["Year"].head())
输出

0    1995
1    1995
Name: Year, dtype: int64
0    1995
1    1995
2    1995
3    1995
4    1995
...
Name: Year, dtype: int64
编辑:按照下面的讨论

import pandas as pd, numpy as np
from io import StringIO

input = """
movieId,title,genres
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
3,Grumpier Old Men (1995),Comedy|Romance
4,Waiting to Exhale (1995),Comedy|Drama|Romance
5,Father of the Bride Part II (1995),Comedy
6,Heat (1995),Action|Crime|Thriller
7,Sabrina (1995),Comedy|Romance
8,Tom and Huck (1995),Adventure|Children
9,Sudden Death (1995),Action
10,GoldenEye (1995),Action|Adventure|Thriller
11,"American President, The (1995)",Comedy|Drama|Romance
12,Dracula: Dead and Loving It (1995),Comedy|Horror
13,Balto (1995),Adventure|Animation|Children
14,Nixon (1995),Drama
"""

df = pd.read_csv(StringIO(input))
df["Year"] = df["title"].apply(lambda title: title[-5:-1])
df['Year'] = df['Year'].dropna().apply(np.int64)
print(df["Year"].head())
输出

0    1995
1    1995
Name: Year, dtype: int64
0    1995
1    1995
2    1995
3    1995
4    1995
...
Name: Year, dtype: int64

请提供“movies_All.csv”?print(recentdf.head(2))=
movieId title Year\1 Toy Story(1995)1995.0 2 Jumanji(1995)1995.0中的一些示例输入/行,谢谢。我想知道这是否是蟒蛇的问题,那么在哪里呢?我尝试了各种不同的方法来更改.datatype(),但仍然返回float64;将(platform.python_version())
打印到您的程序中,并告诉我们您的输出是什么。输出说版本是:3.6.0我现在使用的是3.7.5,但我很确定这不会有什么不同。否则使用
numpy==1.17.3
pandas==0.23.4
。执行我答案中的代码时会发生什么?您是否仍然获得
dtype:float