从日期时间数据类型中提取年份使输出为浮点型 [英] Extracting year from datetime datatypes is giving output as float
本文介绍了从日期时间数据类型中提取年份使输出为浮点型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是熊猫市的新蜂.我遇到的问题需要您的支持我在数据帧中有一个数据时间列,我正试图从中提取年份,但它给出的输出是浮点型的?
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
能请你帮忙吗?
推荐答案
有不同的方法,但以下两种可能会帮助您
There are different ways but below are 2 may help you
这将适用于整个框架
pd.options.display.float_format = '{:,.0f}'.format
这是专栏
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')
在某些情况下,您可能必须先将列转换为str,然后再转换为int
Some cases you may have to convert the columns first to str and then again to int
这篇关于从日期时间数据类型中提取年份使输出为浮点型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文