-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage192.py
More file actions
73 lines (61 loc) · 2.17 KB
/
Copy pathpage192.py
File metadata and controls
73 lines (61 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from datetime import datetime
now = datetime.now()
print(now)
# 手动创建datetime
t1 = datetime.now()
t2 = datetime(1970, 1, 1)
diff = t1 - t2
print(diff)
print(type(diff))
import pandas as pd
ebola = pd.read_csv('country_timeseries.csv')
# 获取左上角数据
print(ebola.iloc[:5, :5])
print(ebola.info())
# 可以创建新列--date_dt,调用to_datetime方法把Date列转换为datetime后赋给date_dt
ebola['date_dt'] = pd.to_datetime(ebola['Date'])
# 还可以显示指定如何把数据转换为datetime对象。to_datetime函数有一个参数format,允许手动指定日期格式
ebola['date_dt'] = pd.to_datetime(ebola['Date'], format = '%m/%d/%Y')
print(ebola.info())
ebola = pd.read_csv('country_timeseries.csv', parse_dates = [0])
print(ebola.info())
d = pd.to_datetime('2016-02-29')
print(d)
print(type(d))
print(d.year)
print(d.month)
print(d.day)
ebola['date_dt'] = pd.to_datetime(ebola['Date'])
print(ebola[['Date', 'date_dt']].head())
ebola['year'] = ebola['date_dt'].dt.year
print(ebola[['Date', 'date_dt', 'year']].head())
ebola['month'], ebola['day'] = (ebola['date_dt'].dt.month,
ebola['date_dt'].dt.day)
print(ebola[['Date', 'date_dt', 'year', 'month', 'day']].head())
print(ebola.info())
print(ebola.iloc[-5:, :5])
print(ebola['date_dt'].min())
ebola['outbreak_d'] = ebola['date_dt'] - ebola['date_dt'].min()
print(ebola[['Date', 'Day', 'outbreak_d']].head())
print(ebola[['Date', 'Day', 'outbreak_d']].tail())
print(ebola.info())
banks = pd.read_csv('banklist.csv')
print(banks.head())
banks = pd.read_csv('banklist.csv', parse_dates = [5, 6])
print(banks.info())
banks['closing_quarter'], banks['closing_year'] = \
(banks['Closing Date'].dt.quarter,
banks['Closing Date'].dt.year)
print(banks.head())
# 可以计算每年破产的银行数量
closing_year = banks.groupby(['closing_year']).size()
print(closing_year)
closing_year_q = banks.groupby(['closing_year', 'closing_quarter']).size()
print(closing_year_q)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax = closing_year.plot()
plt.show()
fig, ax = plt.subplots()
ax = closing_year_q.plot()
plt.show()