Apr-10-2020, 06:36 PM
Hi,
I am working with a pandas dataframe which has a date column, called Occurrence Year. I am trying to only get the year for that column (drop the month and day) so that I can pull the data that is for the year 2015.
This is what the original dataframe looks like...
![[Image: Year.PNG]](https://media.discordapp.net/attachments/696352302386380825/698228483893231696/Year.PNG)
And this is what I tried:
Which gives me NaN values in my column.
So I tried using the .apply code:
I checked the data type of the columns (
and got dtype('O')
Can someone help me, please? I'm at a loss at how to figure this out
I am working with a pandas dataframe which has a date column, called Occurrence Year. I am trying to only get the year for that column (drop the month and day) so that I can pull the data that is for the year 2015.
This is what the original dataframe looks like...
And this is what I tried:
Which gives me NaN values in my column.
So I tried using the .apply code:
NYCrime['Year'] = NYCrime['Occurrence Year'].apply(lambda x: x[:-4])which then gave me the error:
Error:TypeError Traceback (most recent call last)
<ipython-input-47-ef12f8aeb1c4> in <module>
2 #NYCrime['Occurrence Year'].astype(str)
3 #NYCrime['Occurrence Year'].dtype
----> 4 NYCrime['Year'] = NYCrime['Occurrence Year'].apply(lambda x: x[:-4])
5
6 #IGNORE TCrimedf['Neighbourhood'] = TCrimedf['Neighbourhood'].str.rstrip('(0123456789)')
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
3846 else:
3847 values = self.astype(object).values
-> 3848 mapped = lib.map_infer(values, f, convert=convert_dtype)
3849
3850 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-47-ef12f8aeb1c4> in <lambda>(x)
2 #NYCrime['Occurrence Year'].astype(str)
3 #NYCrime['Occurrence Year'].dtype
----> 4 NYCrime['Year'] = NYCrime['Occurrence Year'].apply(lambda x: x[:-4])
5
6 #IGNORE TCrimedf['Neighbourhood'] = TCrimedf['Neighbourhood'].str.rstrip('(0123456789)')
TypeError: 'float' object is not subscriptable
So I tried to use the .astype to change the values to string... NYCrime['Occurrence Year'].astype(str)and run the .apply code again but still got the same error.
I checked the data type of the columns (
NYCrime['Occurrence Year'].dtype)
and got dtype('O')
Can someone help me, please? I'm at a loss at how to figure this out
