python提取字符串中的日期_从pandas列中的日期字符串列中提取年份
我有一个包含日期列的数据集:df.faultDate.head()
0 2014-07-13
1 2014-07-13
2 2014-07-12
3 2014-07-12
4 2014-07-13
我只想从中得到几年。在
^{pr2}$
但是:df.faultDate.apply(dt.datetime.strptime, args=('%Y-%m-%d'))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 df.faultDate.apply(dt.datetime.strptime, args=('%Y-%m-%d'))
/home/shiny/anaconda3/lib/python3.4/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2058 values = lib.map_infer(values, lib.Timestamp)
2059
【python提取字符串中的日期_从pandas列中的日期字符串列中提取年份】-> 2060 mapped = lib.map_infer(values, f, convert=convert_dtype)
2061 if len(mapped) and isinstance(mapped[0], Series):
2062 from pandas.core.frame import DataFrame
pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:58435)()
/home/shiny/anaconda3/lib/python3.4/site-packages/pandas/core/series.py in (x)
2047
2048 if kwds or args and not isinstance(func, np.ufunc):
-> 2049 f = lambda x: func(x, *args, **kwds)
2050 else:
2051 f = func
TypeError: strptime() takes exactly 2 arguments (9 given)
我也不知道在成功地将字符串转换为datetime之后,如何传递.year参数。我可以编写一个包装器函数,将每一行作为输入,然后提取年份,但我认为了解如何应用pandas语法以供将来参考是很有用的。谢谢!在
推荐阅读
- python|python turtle画动物_python turtle 海龟绘图,绘制小猪佩奇-Go语言中文社区
- python微信使用技巧_Python的26个小技巧
- pandas|pandas 如何筛选某一行包含字符串_Pandas过滤dataframe中包含特定字符串的数据方法...
- python|python 逻辑回归分类_Python机器学习(6)——逻辑回归分类
- 用python画路飞代码_路飞学城Python-Day38(第四模块思维导图)
- python日期函数引用_使用python中pandas的read_excel函数将日期保留为字符串
- python|机器学习python——线性回归模型的正规方程(吴恩达课后作业)
- 数据统计与分析|Python使用pandas把字符串转换为日期时间数据
- 推荐|推荐 5 个 yyds 的开源 Python Web 框架
- python顺序结构的表示_Python数据结构之集合(文末赠书)