回答編集履歴

2

修正

2019/02/01 08:30

投稿

can110
can110

スコア38262

test CHANGED
@@ -40,19 +40,21 @@
40
40
 
41
41
  prices = pd.concat(dfs, axis=1)
42
42
 
43
+ prices.columns = map(str,codes) # 列名を変更
44
+
43
45
  print(prices)
44
46
 
45
47
  """
46
48
 
47
- price price price
49
+ 111 222 333
48
50
 
49
51
  date
50
52
 
51
- 2019-01-01 10.0 NaN NaN
53
+ 2019-01-01 10.0 NaN NaN
52
54
 
53
- 2019-01-02 NaN 20.0 NaN
55
+ 2019-01-02 NaN 20.0 NaN
54
56
 
55
- 2019-01-03 NaN NaN 30.0
57
+ 2019-01-03 NaN NaN 30.0
56
58
 
57
59
  """
58
60
 

1

ついでに

2019/02/01 08:30

投稿

can110
can110

スコア38262

test CHANGED
@@ -57,3 +57,55 @@
57
57
  """
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ ### ついでに
64
+
65
+ ふつうに複数の`code`で絞った結果を返したい場合は以下のように書けます。
66
+
67
+ ```Python
68
+
69
+ import sqlite3
70
+
71
+ import pandas as pd
72
+
73
+
74
+
75
+ def get_prices(codes):
76
+
77
+ con = sqlite3.connect('test.db')
78
+
79
+ where = ','.join(map(str,codes)) # codeを直埋め
80
+
81
+ sql = 'SELECT date, price FROM prices WHERE code in ({}) ORDER BY date'.format(where)
82
+
83
+ print(sql) # SELECT date, price FROM prices WHERE code in (111,222,333) ORDER BY date
84
+
85
+ return pd.read_sql(sql, con,
86
+
87
+ parse_dates=('date',),
88
+
89
+ index_col='date')
90
+
91
+
92
+
93
+ prices = get_prices([111,222,333])
94
+
95
+ print(prices)
96
+
97
+ """
98
+
99
+ price
100
+
101
+ date
102
+
103
+ 2019-01-01 10
104
+
105
+ 2019-01-02 20
106
+
107
+ 2019-01-03 30
108
+
109
+ """
110
+
111
+ ```