gpt4 book ai didi

python-3.x - python : Trying to convert single column from Float to Int

转载 作者:行者123 更新时间:2023-12-01 03:00:44 26 4
gpt4 key购买 nike

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

import pandas as pd
import numpy as np

movies_df = pd.read_csv("movies_All.csv")

recentdf = movies_df.copy()

recentdf['Year'] = pd.to_numeric(recentdf['Year'], errors = 'coerce')

recentdf['Year'] = recentdf['Year'].dropna().apply(np.int64)

#recentdf = recentdf[recentdf['Year'] > 2000]

print(recentdf['Year'].head())

输出:名称:年份,dtype:float64

最佳答案

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

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

编辑:按照下面的讨论。

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
2 1995
3 1995
4 1995
...
Name: Year, dtype: int64

关于python-3.x - python : Trying to convert single column from Float to Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com