pandas中的Timestamp只保留日期不显示时间
作者:翻山越岭的猪 时间:2023-12-24 18:45:06
Timestamp只保留日期不显示时间
Timestamp.date()
拿到DataFrame中的一个时间戳后,加一个**.date()**即可
for time in df['日期']):
? ? print(time.date())
pandas从日期属性中提取年月日
在数据挖掘过程中,日期属性是非数值属性, 不能直接输入到模型,将日期属性拆分成年、月和日是必要的。
date属性是object类型的, 通过取单元格可以发现它是字符串类型,这样很容易提取年、月、日
将日期属性拆分成年、月、日
代码如下:
def DateSplit(df, col):
"""
split the object of '2010-01-02' into year(2010), month(1) and day(2).
:param df: to operate data (type:DataFrame)
:param col: column label of date object (type:str)
:return: converted date (type: DataFrame)
"""
year, month, day = [], [], []
data = df.loc[:, col].values
df = df.drop([col], axis=1)
for i in range(data.shape[0]):
year.append(int(data[i][:4]))
month.append(int(data[i][5:7]))
day.append(int(data[i][8:]))
date = pd.DataFrame({'year': year, 'month': month, 'day': day})
result = pd.concat([date, df], axis=1)
return result
pm25_train = pd.read_csv("./datasets_PM25/pm25_train.csv")
data= DateSplit(df=pm25_train,col='date')
data.head(10)
来源:https://blog.csdn.net/piggy0306/article/details/116015054
标签:pandas,Timestamp,保留日期,时间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现飞机大战游戏
2021-09-30 12:05:06
![](https://img.aspxhome.com/file/2023/0/97390_0s.jpg)
Python的re模块正则表达式操作
2023-08-21 11:47:08
python开发App基础操作API使用示例过程
2022-01-19 18:00:35
![](https://img.aspxhome.com/file/2023/5/101115_0s.png)
简述MySQL 正则表达式
2024-01-16 12:17:44
![](https://img.aspxhome.com/file/2023/3/78213_0s.png)
Dreamweaver MX弹出窗口全攻略
2010-09-05 21:14:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311313962s.jpg)
交互设计师心得——核心竞争力
2010-01-19 13:45:00
![](https://img.aspxhome.com/file/UploadPic/20101/19/126eb492886g215-16s.jpg)
vue3 watch和watchEffect的使用以及有哪些区别
2024-05-05 09:11:09
如何用golang运行第一个项目
2024-05-22 10:21:35
![](https://img.aspxhome.com/file/2023/2/123862_0s.jpg)
php全局变量和类配合使用深刻理解
2023-11-18 19:50:17
PyCharm连接远程服务器的超级详细教程
2023-05-10 16:59:31
![](https://img.aspxhome.com/file/2023/6/135256_0s.jpg)
pandas is in和not in的使用说明
2023-04-04 22:06:11
![](https://img.aspxhome.com/file/2023/7/117377_0s.jpg)
带你深入了解SQL Server 2008的独到之处
2009-01-07 14:20:00
Python爬虫之爬取我爱我家二手房数据
2023-08-11 01:35:06
![](https://img.aspxhome.com/file/2023/5/61995_0s.png)
Python实现Pig Latin小游戏实例代码
2022-06-27 04:15:36
利用PyQt5生成过年春联
2023-05-23 21:14:22
![](https://img.aspxhome.com/file/2023/4/75134_0s.jpg)
设计较好付款流程的12个建议
2009-06-08 12:45:00
![](https://img.aspxhome.com/file/UploadPic/20096/8/11-95s.png)
我对PyTorch dataloader里的shuffle=True的理解
2022-04-15 19:22:48
![](https://img.aspxhome.com/file/2023/5/97175_0s.png)
完全卸载MYSQL
2011-02-23 12:11:00
python包的导入方式总结
2021-01-18 08:55:23
python+selenium 点击单选框-radio的实现方法
2023-07-11 01:51:54