Extracting year from datetime datatypes is giving output as float

江枫思渺然 提交于 2021-02-11 15:30:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!