new to Python. Playing around with a large dataset in PythonAnywhere. My CSV for some reason brought in the 'Year' as text. I was able to use pd.to_numeric to make it a number. But now it's a float and I want an int. I tried .dropna().apply(np.int64) but it's still coming in as an int. I need the dropna since apparently there are some missing values code:
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())
Out: Name: Year, dtype: float64
I'm confused. Based on your given input, your code works for me:
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())
Output
0 1995
1 1995
Name: Year, dtype: int64
Edit: Following the discussion below.
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())
Output
0 1995
1 1995
2 1995
3 1995
4 1995
...
Name: Year, dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With