问题
I'm a newbee in Pandas. I need your support in a problem i'm facing I have a datatime column in a dataframe and I'm trying to extract the year from it but the output it is giving is in float?
fifa['Joined_date'].head()
0 1970-01-01
1 1970-01-01
2 1970-01-01
3 1970-01-01
4 1970-01-01
Name: Joined_date, dtype: datetime64[ns]
fifa['Joined_date'].dt.year
0 1970.0
1 1970.0
2 1970.0
3 1970.0
4 1970.0
Name: Joined, Length: 18207, dtype: float64
Output Expected is --> 1970
Can you please help?
回答1:
There are different ways but below are 2 may help you
this will apply on entire frame
pd.options.display.float_format = '{:,.0f}'.format
this is for the column
df['your column'] = df['your column'].astype(int)
if your column has mixed type of data
df['your column'] = df['your column'].astype(int, errors='ignore')
Some cases you may have to convert the columns first to str and then again to int
来源:https://stackoverflow.com/questions/62101429/extracting-year-from-datetime-datatypes-is-giving-output-as-float