当前位置: 首页 > news >正文

解析日期、编码

解析日期

这里指的是将字符串或者object类型的日期,转换成panda或python的日期类型。
主要的是dtype的变化:object / str —> datetime64[ns]

# modules we'll use
import pandas as pd
import numpy as np
import seaborn as sns
import datetime# read in our data
landslides = pd.read_csv("../input/landslide-events/catalog.csv")# set seed for reproducibility
np.random.seed(0)# 直接转变,前提是这一列中的格式已经统一,都是“%m/%d/%y”的形式,如果出现一个2001-01-01那就会失败
# 因此在进行转变前,要先确保格式的统一
# create a new column, date_parsed, with the parsed dates
landslides['date_parsed'] = pd.to_datetime(landslides['date'], format="%m/%d/%y")
landslides['date_parsed'].head()0   2007-03-02
1   2007-03-22
2   2007-04-06
3   2007-04-14
4   2007-04-15
Name: date_parsed, dtype: datetime64[ns]

从日期中获取日

# get the day of the month from the date_parsed column
day_of_month_landslides = landslides['date_parsed'].dt.day
day_of_month_landslides.head()0     2.0
1    22.0
2     6.0
3    14.0
4    15.0
Name: date_parsed, dtype: float64

绘图查看

# remove na's
day_of_month_landslides = day_of_month_landslides.dropna()# plot the day of the month
sns.distplot(day_of_month_landslides, kde=False, bins=31)

在这里插入图片描述

Character Encodings

Avoid UnicoodeDecodeErrors when loading CSV files.

# decode解码变成str类型,str用encode,选择编码类型(utf-8等)变成bytes类型# start with a string
before = "This is the euro symbol: €"# check to see what datatype it is
type(before)
# 默认utf-8编码
# police_killings = pd.read_csv("../input/fatal-police-shootings-in-the-us/PoliceKillingsUS.csv")
# 判断编码
with open("../input/fatal-police-shootings-in-the-us/PoliceKillingsUS.csv", 'rb') as rowdata:result = charset_normalizer.detect(rowdata.read(10000))
print(result)police_killings = pd.read_csv("../input/fatal-police-shootings-in-the-us/PoliceKillingsUS.csv", encoding='Windows-1252')
#  Saving your files with UTF-8 encoding
police_killings.to_csv("my_file_utf8.csv")
http://www.lryc.cn/news/472861.html

相关文章:

  • 【Qt】QApplication::restoreOverrideCursor():恢复鼠标光标到原始状态的用法解析
  • 重生之“我打数据结构,真的假的?”--2.单链表(无习题)
  • 【有啥问啥】视频插帧算法技术原理详解
  • Leetcode148,109以及二者的合并 -> Tencent面试算法题 - 无序双向链表转BST
  • 【蓝桥杯选拔赛真题77】python计算小球 第十五届青少年组蓝桥杯python选拔赛真题 算法思维真题解析
  • 获取Hive表备注
  • 10.30学习
  • 什么是栈溢出
  • 在linux中arm-linux-gcc和/usr/bin/gcc有啥区别
  • 常用环境部署(二十二)——MySQL的数据库迁移到另一个机器上
  • 两台主机只能单方向ping通
  • redis windows 5.0 下载
  • 视频转gif怎么转换?6种视频格式转换简单方法分享,附操作截图!
  • StructRAG简介
  • java脚手架系列12-mongoDB
  • python四舍五入保留两位小数
  • 期权懂|有什么期权交易策略能够稳赚不赔的?
  • 笔记本脱机状态
  • Node.js:模块 包
  • 油动无人机动力测试台-60公斤级-Flight Stand 60 ICE
  • 给grasshopper中的python脚本电池加个标签
  • 别被忽悠了 Lua 数组真的也可以从 0 开始索引?
  • docker占用磁盘过多问题
  • [实时计算flink]使用Python依赖
  • MySql如何实现分布式锁
  • 「行内揭秘」 SQLynx数据库界的“小众宝藏”?
  • 【已解决】【MySQL】IDEA配置数据库 报错 未配置SQL方言 无法使用SQL提示
  • js 通过filter 实现扁平化数据tree
  • Android 开发 调节声音 SeekBar自定义样式
  • UART-通用异步收发器