没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
在机器学习过程中,通常会通过pandas读取csv文件,保持成dadaframe格式,然而有时候需要对dataframe中的时间字段进行数据建模,比如时间格式为datetime,那么像一般操作dataframe的方式来操作时间字段会报错的,所以在使用sklearn库进行fit和predict的时候,通常要把时间字段首先转换为timestamp格式,在fit和predict之后,如果需要matplotlib绘图的时候,再把timestamp格式转换为时间字符串,比如2017-02-01 14:25:14。 下面是我处理过的一段代码,希望可以帮到童鞋们! doc_list1 = [] for i
资源详情
资源评论
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/14863202/bg1.jpg)
python 处理处理dataframe中的时间字段方法中的时间字段方法
在机器学习过程中,通常会通过pandas读取csv文件,保持成dadaframe格式,然而有时候需要对dataframe中的时间字段进行
数据建模,比如时间格式为datetime,那么像一般操作dataframe的方式来操作时间字段会报错的,所以在使用sklearn库进行
fit和predict的时候,通常要把时间字段首先转换为timestamp格式,在fit和predict之后,如果需要matplotlib绘图的时候,再把
timestamp格式转换为时间字符串,比如2017-02-01 14:25:14。
下面是我处理过的一段代码,希望可以帮到童鞋们!下面是我处理过的一段代码,希望可以帮到童鞋们!
doc_list1 = [] for i in doc1.iloc[:,1:2].values.tolist(): # 转换成了时间戳格式
for j in i:
dt = time.strptime(j, "%Y-%m-%d %H:%M:%S")
dt_new = time.mktime(dt)
doc_list1.append(dt_new)
doc_list2 = [] for i in doc_list1:
time_local = time.localtime(i)
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
dt1 = datetime.datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
doc_list2.append(dt1)
X1 = np.mat(doc_list1).T
y1= test_target1001
clf = AdaBoostRegressor(DecisionTreeRegressor(max_depth=5),n_estimators=1000, random_state=rng)
clf.fit(X1,y1)
yhat1 = clf.predict(X1)
补充一下:如果补充一下:如果value不是不是datetime格式还需要进行转换格式还需要进行转换
value = result.iloc[:,1] list = [] for i in value:
print(type(i.to_pydatetime().timetuple()),i)
list.append(time.mktime(i.to_datetime().timetuple()))
print(list)
以上这篇python 处理dataframe中的时间字段方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多
多支持软件开发网。
您可能感兴趣的文章您可能感兴趣的文章:pandas把dataframe转成Series,改变列中值的类型方法python pandas中DataFrame类型数据操作函数的
方法python读取文本中数据并转化为DataFrame的实例python的dataframe转换为多维矩阵的方法python DataFrame 修改列的
顺序实例python dataframe astype 字段类型转换方法
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38519849
- 粉丝: 5
- 资源: 973
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论10