pandas - 从刮擦中清除日期
问题描述
我正在抓取一个 HMTL 表,最终数据框创建了一个需要清理和格式化的“日期”列。
我的范围是将此列转换为数据列。
在我的数据框下方:
在这一步之后我想做的就是清理Date
列并将此列转换为 pandas 日期列。
有什么帮助吗?
以下是生成此表的方法:
## web scrapping
import requests
import lxml.html as lh
import pandas as pd
url='https://markets.ft.com/data/funds/tearsheet/historical?s=LU0841585341:GBP'
#Create a handle, page, to handle the contents of the website
page = requests.get(url)
#Store the contents of the website under doc
doc = lh.fromstring(page.content)
#Parse data that are stored between <tr>..</tr> of HTML
tr_elements = doc.xpath('//tr')
#Create empty list
col=[]
i=0
#For each row, store each first element (header) and an empty list
for t in tr_elements[0]:
i+=1
name=t.text_content()
# print '%d:"%s"'%(i,name)
col.append((name,[]))
#Since out first row is the header, data is stored on the second row onwards
for j in range(1,len(tr_elements)):
#T is our j'th row
T=tr_elements[j]
#If row is not of size 10, the //tr data is not from our table
if len(T)!=6:
break
#i is the index of our column
i=0
#Iterate through each element of the row
for t in T.iterchildren():
data=t.text_content()
#Check if row is empty
if i>0:
#Convert any numerical value to integers
try:
data=int(data)
except:
pass
#Append the data to the empty list of the i'th column
col[i][1].append(data)
#Increment i for the next column
i+=1
Dict={title:column for (title,column) in col}
df=pd.DataFrame(Dict)
df.head()
解决方案
你可以做:
df["Date"] = pd.to_datetime(
df["Date"].str.replace(r"(\d+)([A-Z].*)", r"\1", regex=True)
)
print(df)
印刷:
Date Open High Low Close Volume
0 2021-09-10 27.28 27.28 27.28 27.28 ----
1 2021-09-09 27.35 27.35 27.35 27.35 ----
2 2021-09-08 27.42 27.42 27.42 27.42 ----
3 2021-09-07 27.54 27.54 27.54 27.54 ----
4 2021-09-03 27.44 27.44 27.44 27.44 ----
5 2021-09-02 27.48 27.48 27.48 27.48 ----
6 2021-09-01 27.26 27.26 27.26 27.26 ----
7 2021-08-31 27.31 27.31 27.31 27.31 ----
8 2021-08-30 27.46 27.46 27.46 27.46 ----
9 2021-08-27 27.32 27.32 27.32 27.32 ----
10 2021-08-26 27.23 27.23 27.23 27.23 ----
11 2021-08-25 27.27 27.27 27.27 27.27 ----
12 2021-08-24 27.22 27.22 27.22 27.22 ----
13 2021-08-23 27.05 27.05 27.05 27.05 ----
14 2021-08-20 26.92 26.92 26.92 26.92 ----
15 2021-08-19 26.58 26.58 26.58 26.58 ----
16 2021-08-18 26.62 26.62 26.62 26.62 ----
17 2021-08-17 26.63 26.63 26.63 26.63 ----
18 2021-08-16 26.56 26.56 26.56 26.56 ----
19 2021-08-13 26.77 26.77 26.77 26.77 ----
20 2021-08-12 26.67 26.67 26.67 26.67 ----
推荐阅读
- ruby - `File.enum_for(:readlines, ...)` 未枚举
- excel - 修剪整个工作表中的单元格,溢出错误
- regex - 正则表达式 - 匹配特定字符串、起始值、结束值以及介于两者之间的所有内容
- python - 如何在 Jupyter notebook 中制作 Tufte 风格的旁注?
- verilog - 使用 dff verilog 的移位寄存器
- node.js - ENOTSUP:套接字不支持操作
- pic - 延迟子程序时间计算
- javascript - 将变换 attr 的每个 X 值增加 50
- hyperledger-fabric - 从节点后端连接到 Fabric 导致主机名/IP 与证书不匹配
- npm - npm 错误!enoent ENOENT:没有这样的文件或目录,chmod