数据可视化 第2章
第2章 过滤数据
1.transform_date函数把utc格式时间转换为QDateTime类型
def transform_date(utc, timezone=None):
utc_fmt = "yyyy-MM-ddTHH:mm:ss.zzzZ"
new_date = QDateTime().fromString(utc, utc_fmt)
# print (new_date.toString(utc_fmt))
if timezone:
new_date.setTimeZone(timezone)
return new_date
2.drop函数过滤数据,mag<0的行都被删除
#移除错误的mag
df = df.drop(df[df.mag < 0].index)
magnitudes = df["mag"]
3.创建本地时区
#本地时区
timezone = QTimeZone(b"Asia/Beijing")
4.apply方法能支持lambda表达式,能把函数应用到每行数据上,我们提取time列数据,转换为QDateTime类型
#将时间戳转换为本地时区
times = df["time"].apply(lambda x: transform_date(x, timezone))
5.完整代码
# This Python file uses the following encoding: utf-8
import sys, argparse
import pandas as pd
from PySide2.QtCore import QDateTime, QTimeZone
def transform_date(utc, timezone=None):
utc_fmt = "yyyy-MM-ddTHH:mm:ss.zzzZ"
new_date = QDateTime().fromString(utc, utc_fmt)
# print (new_date.toString(utc_fmt))
if timezone:
new_date.setTimeZone(timezone)
return new_date
def read_data(fname):
df = pd.read_csv(fname)
#移除错误的mag
df = df.drop(df[df.mag < 0].index)
magnitudes = df["mag"]
#本地时区
timezone = QTimeZone(b"Asia/Beijing")
#将时间戳转换为本地时区
times = df["time"].apply(lambda x: transform_date(x, timezone))
return times, magnitudes
if __name__ == "__main__":
options = argparse.ArgumentParser()
options.description = "过滤csv信息"
options.add_argument("-f", "--file", type=str, required=True, help="CSV文件路径")
args = options.parse_args()
data = read_data(args.file)
print (data)
6.控制台输出log
(0 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 18, 4...
1 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 10, 1...
2 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 7, 3,...
3 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 6, 4,...
4 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 5, 43...
5 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 4, 57...
6 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 4, 27...
7 PySide2.QtCore.QDateTime(2019, 7, 24, 9, 1, 27...
8 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 54, 5...
9 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 54, 1...
10 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 53, 2...
11 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 52, 2...
12 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 50, 1...
13 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 49, 1...
14 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 48, 4...
15 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 47, 3...
16 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 46, 5...
17 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 44, 5...
18 PySide2.QtCore.QDateTime(2019, 7, 24, 8, 44, 0...
Name: time, dtype: object, 0 1.37
1 0.49
2 1.43
3 2.73
4 2.04
5 1.44
6 1.23
7 0.75
8 1.22
9 1.20
10 0.92
11 1.67
12 1.50
13 1.20
14 1.02
15 1.51
16 1.41
17 0.87
18 0.91
Name: mag, dtype: float64)