6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SENSYAdvent Calendar 2017

Day 7

Pandas のよく使うスニペット集

Last updated at Posted at 2017-12-06

当記事は、SENSY Advent Calendar 2017の7日目の記事です。

前職や今在籍しているSENSY株式会社ではずっとウェブの開発をしていましたが、11月からAIチームのほうに異動しました。毎日ドキドキしています。

当記事は、最近よく使うPandasのスニペット集をまとめていきたいと思います。元々は自分用に参照できるページがほしいなと思っていたので、Pandas自体の説明や細かい説明等は他の記事に譲って、ひたすらスニペットを列挙していきます(いいスニペットを見つけたら追加していきます)。
また、使用するデータはInvesting.comのBTC/JPYから取得しています。

インポート

Pandas をインポートするには以下を実行します。
pd としてインポートすることが多いみたいです。

import pandas as pd

DataFrameの作成

DataFrame を作成するには以下のようにします。

df = pd.DataFrame([
    ['2017-10-01', 488197, 489003, 489723, 466627],
    ['2017-10-02', 489139, 488197, 493590, 473007],
    ['2017-10-03', 487987, 489139, 495358, 478978],
    ['2017-10-04', 480618, 487987, 491477, 472229],
    ['2017-10-05', 490673, 480618, 491379, 468064],
    ['2017-10-06', 487560, 490673, 496803, 469707],
    ['2017-10-07', 494927, 487560, 495221, 482922],
    ['2017-10-08', 502668, 494927, 513768, 489213],
    ['2017-10-09', 521692, 502668, 539816, 497525],
    ['2017-10-10', 529605, 521316, 543653, 509183],
    ['2017-10-11', 535745, 529605, 579834, 522659],
    ['2017-10-12', 603830, 535745, 608320, 526535],
    ['2017-10-13', 626833, 603830, 679763, 535514],
    ['2017-10-14', 641376, 626833, 679763, 579459],
    ['2017-10-15', 640736, 641376, 649894, 613526],
    ['2017-10-16', 646682, 640736, 657214, 613343],
    ['2017-10-17', 634682, 646682, 656551, 628475],
    ['2017-10-18', 638945, 634682, 656831, 597587],
    ['2017-10-19', 639563, 638945, 652154, 595464],
    ['2017-10-20', 680935, 639563, 681990, 634207],
    ['2017-10-21', 683720, 680935, 705656, 642202],
    ['2017-10-22', 674718, 683720, 705573, 666582],
    ['2017-10-23', 688504, 674718, 699988, 657828],
    ['2017-10-24', 647263, 688504, 717500, 635624],
    ['2017-10-25', 661909, 647263, 675796, 630626],
    ['2017-10-26', 656882, 661909, 692724, 617153],
    ['2017-10-27', 662773, 672772, 692997, 652498],
    ['2017-10-28', 663574, 662773, 681126, 652498],
    ['2017-10-29', 704915, 663574, 711090, 653967],
    ['2017-10-30', 689863, 704915, 718159, 656695],
    ['2017-10-31', 729017, 689863, 733024, 687718]
], columns=['date', 'open', 'high', 'low', 'close'])

df

# 		date		open	high	low		close
# 0		2017-10-01	488197	489003	489723	466627
# 1		2017-10-02	489139	488197	493590	473007
# 2		2017-10-03	487987	489139	495358	478978
# 3		2017-10-04	480618	487987	491477	472229
# 4		2017-10-05	490673	480618	491379	468064
# 5		2017-10-06	487560	490673	496803	469707
# 6		2017-10-07	494927	487560	495221	482922
# 7		2017-10-08	502668	494927	513768	489213
# 8		2017-10-09	521692	502668	539816	497525
# 9		2017-10-10	529605	521316	543653	509183
# 10	2017-10-11	535745	529605	579834	522659
# 11	2017-10-12	603830	535745	608320	526535
# 12	2017-10-13	626833	603830	679763	535514
# 13	2017-10-14	641376	626833	679763	579459
# 14	2017-10-15	640736	641376	649894	613526
# 15	2017-10-16	646682	640736	657214	613343
# 16	2017-10-17	634682	646682	656551	628475
# 17	2017-10-18	638945	634682	656831	597587
# 18	2017-10-19	639563	638945	652154	595464
# 19	2017-10-20	680935	639563	681990	634207
# 20	2017-10-21	683720	680935	705656	642202
# 21	2017-10-22	674718	683720	705573	666582
# 22	2017-10-23	688504	674718	699988	657828
# 23	2017-10-24	647263	688504	717500	635624
# 24	2017-10-25	661909	647263	675796	630626
# 25	2017-10-26	656882	661909	692724	617153
# 26	2017-10-27	662773	672772	692997	652498
# 27	2017-10-28	663574	662773	681126	652498
# 28	2017-10-29	704915	663574	711090	653967
# 29	2017-10-30	689863	704915	718159	656695
# 30	2017-10-31	729017	689863	733024	687718

DataFrameの情報を取得

dtypesを表示

df.dtypes

# date     object
# open     int64 
# high     int64 
# low      int64 
# close    int64 
# dtype: object

列情報を表示

df.columns

# Index(['date', 'open', 'high', 'low', 'close'], dtype='object')

行と列を表示

df.shape

# (31, 5)

Numpy行列を取得

df.values

# array([['2017-10-01', 488197, 489003, 489723, 466627],
#        ['2017-10-02', 489139, 488197, 493590, 473007],
#        ['2017-10-03', 487987, 489139, 495358, 478978],
#        ['2017-10-04', 480618, 487987, 491477, 472229],
#        ['2017-10-05', 490673, 480618, 491379, 468064],
#        ['2017-10-06', 487560, 490673, 496803, 469707],
#        ['2017-10-07', 494927, 487560, 495221, 482922],
#        ['2017-10-08', 502668, 494927, 513768, 489213],
#        ['2017-10-09', 521692, 502668, 539816, 497525],
#        ['2017-10-10', 529605, 521316, 543653, 509183],
#        ['2017-10-11', 535745, 529605, 579834, 522659],
#        ['2017-10-12', 603830, 535745, 608320, 526535],
#        ['2017-10-13', 626833, 603830, 679763, 535514],
#        ['2017-10-14', 641376, 626833, 679763, 579459],
#        ['2017-10-15', 640736, 641376, 649894, 613526],
#        ['2017-10-16', 646682, 640736, 657214, 613343],
#        ['2017-10-17', 634682, 646682, 656551, 628475],
#        ['2017-10-18', 638945, 634682, 656831, 597587],
#        ['2017-10-19', 639563, 638945, 652154, 595464],
#        ['2017-10-20', 680935, 639563, 681990, 634207],
#        ['2017-10-21', 683720, 680935, 705656, 642202],
#        ['2017-10-22', 674718, 683720, 705573, 666582],
#        ['2017-10-23', 688504, 674718, 699988, 657828],
#        ['2017-10-24', 647263, 688504, 717500, 635624],
#        ['2017-10-25', 661909, 647263, 675796, 630626],
#        ['2017-10-26', 656882, 661909, 692724, 617153],
#        ['2017-10-27', 662773, 672772, 692997, 652498],
#        ['2017-10-28', 663574, 662773, 681126, 652498],
#        ['2017-10-29', 704915, 663574, 711090, 653967],
#        ['2017-10-30', 689863, 704915, 718159, 656695],
#        ['2017-10-31', 729017, 689863, 733024, 687718]], dtype=object)

要約情報を表示

df.describe()

# 		open			high			low				close
# count	31.000000		31.000000		31.000000		31.000000
# mean	604049.387097	596807.451613	622152.741935	574439.193548
# std	82049.462224	81639.444429	88161.693574	76040.954856
# min	480618.000000	480618.000000	489723.000000	466627.000000
# 25%	512180.000000	498797.500000	526792.000000	493369.000000
# 50%	639563.000000	638945.000000	656831.000000	597587.000000
# 75%	663173.500000	663173.500000	692860.500000	638913.000000
# max	729017.000000	704915.000000	733024.000000	687718.000000

平均を表示

df.mean()

# open     604049.387097
# high     596807.451613
# low      622152.741935
# close    574439.193548
# dtype: float64

行の抽出

スライスを使って部分的に取得する方法

df[2:4]

# 		date	open	high	low		close
# 2	2017-10-03	487987	489139	495358	478978
# 3	2017-10-04	480618	487987	491477	472229

条件を指定する方法

df[df.date == '2017-10-21']

# 		date		open	high	low		close
# 20	2017-10-21	683720	680935	705656	642202

df[df.open > 700000]

# 		date		open	high	low		close
# 28	2017-10-29	704915	663574	711090	653967
# 30	2017-10-31	729017	689863	733024	687718

先頭のN行を取得

df.head(3)

# 	date		open	high	low		close
# 0	2017-10-01	488197	489003	489723	466627
# 1	2017-10-02	489139	488197	493590	473007
# 2	2017-10-03	487987	489139	495358	478978

最後からN行を取得

df.tail(3)

# 		date		open	high	low		close
# 28	2017-10-29	704915	663574	711090	653967
# 29	2017-10-30	689863	704915	718159	656695
# 30	2017-10-31	729017	689863	733024	687718

イテレーションを回して1行ずつ取得

for index, row in df.iterrows():
    print(row)

# date     2017-10-01
# open     488197    
# high     489003    
# low      489723    
# close    466627    
# Name: 0, dtype: object
# date     2017-10-02
# open     489139    
# high     488197    
# low      493590    
# close    473007    
# Name: 1, dtype: object

...

# date     2017-10-31
# open     729017    
# high     689863    
# low      733024    
# close    687718    
# Name: 30, dtype: object

列の抽出

df['date']

# 0     2017-10-01
# 1     2017-10-02
# 2     2017-10-03
# 3     2017-10-04
# 4     2017-10-05
# 5     2017-10-06
# 6     2017-10-07
# 7     2017-10-08
# 8     2017-10-09
# 9     2017-10-10
# 10    2017-10-11
# 11    2017-10-12
# 12    2017-10-13
# 13    2017-10-14
# 14    2017-10-15
# 15    2017-10-16
# 16    2017-10-17
# 17    2017-10-18
# 18    2017-10-19
# 19    2017-10-20
# 20    2017-10-21
# 21    2017-10-22
# 22    2017-10-23
# 23    2017-10-24
# 24    2017-10-25
# 25    2017-10-26
# 26    2017-10-27
# 27    2017-10-28
# 28    2017-10-29
# 29    2017-10-30
# 30    2017-10-31
# Name: date, dtype: object

df.open

# 0     488197
# 1     489139
# 2     487987
# 3     480618
# 4     490673
# 5     487560
# 6     494927
# 7     502668
# 8     521692
# 9     529605
# 10    535745
# 11    603830
# 12    626833
# 13    641376
# 14    640736
# 15    646682
# 16    634682
# 17    638945
# 18    639563
# 19    680935
# 20    683720
# 21    674718
# 22    688504
# 23    647263
# 24    661909
# 25    656882
# 26    662773
# 27    663574
# 28    704915
# 29    689863
# 30    729017
# Name: open, dtype: int64

列名の変更

df.rename(columns = {
    'open': 'opening',
    'close': 'closing',
})

# 		date		opening	high	low		closing
# 0		2017-10-01	488197	489003	489723	466627
# 1		2017-10-02	489139	488197	493590	473007
# 2		2017-10-03	487987	489139	495358	478978
# 3		2017-10-04	480618	487987	491477	472229
# 4		2017-10-05	490673	480618	491379	468064
# 5		2017-10-06	487560	490673	496803	469707
# 6		2017-10-07	494927	487560	495221	482922
# 7		2017-10-08	502668	494927	513768	489213
# 8		2017-10-09	521692	502668	539816	497525
# 9		2017-10-10	529605	521316	543653	509183
# 10	2017-10-11	535745	529605	579834	522659
# 11	2017-10-12	603830	535745	608320	526535
# 12	2017-10-13	626833	603830	679763	535514
# 13	2017-10-14	641376	626833	679763	579459
# 14	2017-10-15	640736	641376	649894	613526
# 15	2017-10-16	646682	640736	657214	613343
# 16	2017-10-17	634682	646682	656551	628475
# 17	2017-10-18	638945	634682	656831	597587
# 18	2017-10-19	639563	638945	652154	595464
# 19	2017-10-20	680935	639563	681990	634207
# 20	2017-10-21	683720	680935	705656	642202
# 21	2017-10-22	674718	683720	705573	666582
# 22	2017-10-23	688504	674718	699988	657828
# 23	2017-10-24	647263	688504	717500	635624
# 24	2017-10-25	661909	647263	675796	630626
# 25	2017-10-26	656882	661909	692724	617153
# 26	2017-10-27	662773	672772	692997	652498
# 27	2017-10-28	663574	662773	681126	652498
# 28	2017-10-29	704915	663574	711090	653967
# 29	2017-10-30	689863	704915	718159	656695
# 30	2017-10-31	729017	689863	733024	687718

一括処理する場合の例:

df.columns = map(str.upper, df.columns)

df

#		DATE		OPEN	HIGH	LOW		CLOSE
# 0		2017-10-01	488197	489003	489723	466627
# 1		2017-10-02	489139	488197	493590	473007
# 2		2017-10-03	487987	489139	495358	478978
# 3		2017-10-04	480618	487987	491477	472229
# 4		2017-10-05	490673	480618	491379	468064
# 5		2017-10-06	487560	490673	496803	469707
# 6		2017-10-07	494927	487560	495221	482922
# 7		2017-10-08	502668	494927	513768	489213
# 8		2017-10-09	521692	502668	539816	497525
# 9		2017-10-10	529605	521316	543653	509183
# 10	2017-10-11	535745	529605	579834	522659
# 11	2017-10-12	603830	535745	608320	526535
# 12	2017-10-13	626833	603830	679763	535514
# 13	2017-10-14	641376	626833	679763	579459
# 14	2017-10-15	640736	641376	649894	613526
# 15	2017-10-16	646682	640736	657214	613343
# 16	2017-10-17	634682	646682	656551	628475
# 17	2017-10-18	638945	634682	656831	597587
# 18	2017-10-19	639563	638945	652154	595464
# 19	2017-10-20	680935	639563	681990	634207
# 20	2017-10-21	683720	680935	705656	642202
# 21	2017-10-22	674718	683720	705573	666582
# 22	2017-10-23	688504	674718	699988	657828
# 23	2017-10-24	647263	688504	717500	635624
# 24	2017-10-25	661909	647263	675796	630626
# 25	2017-10-26	656882	661909	692724	617153
# 26	2017-10-27	662773	672772	692997	652498
# 27	2017-10-28	663574	662773	681126	652498
# 28	2017-10-29	704915	663574	711090	653967
# 29	2017-10-30	689863	704915	718159	656695
# 30	2017-10-31	729017	689863	733024	687718

列の削除

df.drop(['date'], axis=1)

# 		open	high	low		close
# 0		488197	489003	489723	466627
# 1		489139	488197	493590	473007
# 2		487987	489139	495358	478978
# 3		480618	487987	491477	472229
# 4		490673	480618	491379	468064
# 5		487560	490673	496803	469707
# 6		494927	487560	495221	482922
# 7		502668	494927	513768	489213
# 8		521692	502668	539816	497525
# 9		529605	521316	543653	509183
# 10	535745	529605	579834	522659
# 11	603830	535745	608320	526535
# 12	626833	603830	679763	535514
# 13	641376	626833	679763	579459
# 14	640736	641376	649894	613526
# 15	646682	640736	657214	613343
# 16	634682	646682	656551	628475
# 17	638945	634682	656831	597587
# 18	639563	638945	652154	595464
# 19	680935	639563	681990	634207
# 20	683720	680935	705656	642202
# 21	674718	683720	705573	666582
# 22	688504	674718	699988	657828
# 23	647263	688504	717500	635624
# 24	661909	647263	675796	630626
# 25	656882	661909	692724	617153
# 26	662773	672772	692997	652498
# 27	663574	662773	681126	652498
# 28	704915	663574	711090	653967
# 29	689863	704915	718159	656695
# 30	729017	689863	733024	687718

列をDatetime型に変換

df.date

# 0     2017-10-01
# 1     2017-10-02
# 2     2017-10-03
# 3     2017-10-04
# 4     2017-10-05
# 5     2017-10-06
# 6     2017-10-07
# 7     2017-10-08
# 8     2017-10-09
# 9     2017-10-10
# 10    2017-10-11
# 11    2017-10-12
# 12    2017-10-13
# 13    2017-10-14
# 14    2017-10-15
# 15    2017-10-16
# 16    2017-10-17
# 17    2017-10-18
# 18    2017-10-19
# 19    2017-10-20
# 20    2017-10-21
# 21    2017-10-22
# 22    2017-10-23
# 23    2017-10-24
# 24    2017-10-25
# 25    2017-10-26
# 26    2017-10-27
# 27    2017-10-28
# 28    2017-10-29
# 29    2017-10-30
# 30    2017-10-31
# Name: date, dtype: object

df['date'] = pd.to_datetime(df['date'])

df.date

# 0    2017-10-01
# 1    2017-10-02
# 2    2017-10-03
# 3    2017-10-04
# 4    2017-10-05
# 5    2017-10-06
# 6    2017-10-07
# 7    2017-10-08
# 8    2017-10-09
# 9    2017-10-10
# 10   2017-10-11
# 11   2017-10-12
# 12   2017-10-13
# 13   2017-10-14
# 14   2017-10-15
# 15   2017-10-16
# 16   2017-10-17
# 17   2017-10-18
# 18   2017-10-19
# 19   2017-10-20
# 20   2017-10-21
# 21   2017-10-22
# 22   2017-10-23
# 23   2017-10-24
# 24   2017-10-25
# 25   2017-10-26
# 26   2017-10-27
# 27   2017-10-28
# 28   2017-10-29
# 29   2017-10-30
# 30   2017-10-31
# Name: date, dtype: datetime64[ns]

データ参照

df[df.date == '2017-10-20'][['date', 'open']]

# 		date		open
# 19	2017-10-20	680935
df.loc[5:8, ['date', 'open', 'close']]

# 	date		open	close
# 5	2017-10-06	487560	469707
# 6	2017-10-07	494927	482922
# 7	2017-10-08	502668	489213
# 8	2017-10-09	521692	497525
df.iloc[5:8, 0:3]

# 	date		open	high
# 5	2017-10-06	487560	490673
# 6	2017-10-07	494927	487560
# 7	2017-10-08	502668	494927
df.query('open > 70000 & close > 680000')

# 		date		open	high	low		close
# 30	2017-10-31	729017	689863	733024	687718

ソート

df.sort_values(by='open', ascending=True).head(5)

# 	date		open	high	low		close
# 3	2017-10-04	480618	487987	491477	472229
# 5	2017-10-06	487560	490673	496803	469707
# 2	2017-10-03	487987	489139	495358	478978
# 0	2017-10-01	488197	489003	489723	466627
# 1	2017-10-02	489139	488197	493590	473007

df.sort_values(by='open', ascending=False).head(5)

# 		date		open	high	low		close
# 30	2017-10-31	729017	689863	733024	687718
# 28	2017-10-29	704915	663574	711090	653967
# 29	2017-10-30	689863	704915	718159	656695
# 22	2017-10-23	688504	674718	699988	657828
# 20	2017-10-21	683720	680935	705656	642202

DataFrameのインデックス

インデックスを取得

df.index

# RangeIndex(start=0, stop=31, step=1)

インデックスのリセット

df = df.sort_values(by='high', ascending=False)

df.head(5)

# 		date		open	high	low		close
# 29	2017-10-30	689863	704915	718159	656695
# 30	2017-10-31	729017	689863	733024	687718
# 23	2017-10-24	647263	688504	717500	635624
# 21	2017-10-22	674718	683720	705573	666582
# 20	2017-10-21	683720	680935	705656	642202

df = df.sort_values(by='high', ascending=False)

new_df = df.reset_index(drop=True)

new_df.head(5)

# 	date		open	high	low		close
# 0	2017-10-30	689863	704915	718159	656695
# 1	2017-10-31	729017	689863	733024	687718
# 2	2017-10-24	647263	688504	717500	635624
# 3	2017-10-22	674718	683720	705573	666582
# 4	2017-10-21	683720	680935	705656	642202

ピボットテーブル

インデックスをdate列、カラムをlow列、値をhigh列として、欠損値は0.0で埋めて、ピボットテーブルを作成

pd.pivot_table(df, values='high', index='date', columns='low', fill_value=0.0)

# low	489723	491379	491477	493590	495221	495358	496803	513768	539816	543653	...	681990	692724	692997	699988	705573	705656	711090	717500	718159	733024
# date																					
# 2017-10-01	489003	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-02	0	0	0	488197	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-03	0	0	0	0	0	489139	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-04	0	0	487987	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-05	0	480618	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-06	0	0	0	0	0	0	490673	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-07	0	0	0	0	487560	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-08	0	0	0	0	0	0	0	494927	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-09	0	0	0	0	0	0	0	0	502668	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-10	0	0	0	0	0	0	0	0	0	521316	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-11	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-12	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-13	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-14	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-15	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-16	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-17	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-18	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-19	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-20	0	0	0	0	0	0	0	0	0	0	...	639563	0	0	0	0	0	0	0	0	0
# 2017-10-21	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	680935	0	0	0	0
# 2017-10-22	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	683720	0	0	0	0	0
# 2017-10-23	0	0	0	0	0	0	0	0	0	0	...	0	0	0	674718	0	0	0	0	0	0
# 2017-10-24	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	688504	0	0
# 2017-10-25	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-26	0	0	0	0	0	0	0	0	0	0	...	0	661909	0	0	0	0	0	0	0	0
# 2017-10-27	0	0	0	0	0	0	0	0	0	0	...	0	0	672772	0	0	0	0	0	0	0
# 2017-10-28	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	0
# 2017-10-29	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	663574	0	0	0
# 2017-10-30	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	704915	0
# 2017-10-31	0	0	0	0	0	0	0	0	0	0	...	0	0	0	0	0	0	0	0	0	689863

連結

行方向連結

new_df = pd.DataFrame([
    ['2017-11-01', 729017, 787179, 693575, 775693],
    ['2017-11-02', 775693, 869998, 707769, 805927],
    ['2017-11-03', 781055, 904306, 777738, 828976],
    ['2017-11-04', 828976, 889949, 789065, 861002],
    ['2017-11-05', 861002, 865634, 818642, 844983],
    ['2017-11-06', 844983, 865141, 791010, 797783],
    ['2017-11-07', 797783, 856412, 787884, 827687],
    ['2017-11-08', 827687, 890823, 807321, 866073],
    ['2017-11-09', 866073, 890715, 806285, 806632],
    ['2017-11-10', 806632, 866885, 751096, 765882]
], columns=['date', 'open', 'high', 'low', 'close'])

pd.concat([df, new_df])

# 		date		open	high	low		close
# 0		2017-10-01	488197	489003	489723	466627
# 1		2017-10-02	489139	488197	493590	473007
# 2		2017-10-03	487987	489139	495358	478978
# 3		2017-10-04	480618	487987	491477	472229
# 4		2017-10-05	490673	480618	491379	468064
# 5		2017-10-06	487560	490673	496803	469707
# 6		2017-10-07	494927	487560	495221	482922
# 7		2017-10-08	502668	494927	513768	489213
# 8		2017-10-09	521692	502668	539816	497525
# 9		2017-10-10	529605	521316	543653	509183
# 10	2017-10-11	535745	529605	579834	522659
# 11	2017-10-12	603830	535745	608320	526535
# 12	2017-10-13	626833	603830	679763	535514
# 13	2017-10-14	641376	626833	679763	579459
# 14	2017-10-15	640736	641376	649894	613526
# 15	2017-10-16	646682	640736	657214	613343
# 16	2017-10-17	634682	646682	656551	628475
# 17	2017-10-18	638945	634682	656831	597587
# 18	2017-10-19	639563	638945	652154	595464
# 19	2017-10-20	680935	639563	681990	634207
# 20	2017-10-21	683720	680935	705656	642202
# 21	2017-10-22	674718	683720	705573	666582
# 22	2017-10-23	688504	674718	699988	657828
# 23	2017-10-24	647263	688504	717500	635624
# 24	2017-10-25	661909	647263	675796	630626
# 25	2017-10-26	656882	661909	692724	617153
# 26	2017-10-27	662773	672772	692997	652498
# 27	2017-10-28	663574	662773	681126	652498
# 28	2017-10-29	704915	663574	711090	653967
# 29	2017-10-30	689863	704915	718159	656695
# 30	2017-10-31	729017	689863	733024	687718
# 0		2017-11-01	729017	787179	693575	775693
# 1		2017-11-02	775693	869998	707769	805927
# 2		2017-11-03	781055	904306	777738	828976
# 3		2017-11-04	828976	889949	789065	861002
# 4		2017-11-05	861002	865634	818642	844983
# 5		2017-11-06	844983	865141	791010	797783
# 6		2017-11-07	797783	856412	787884	827687
# 7		2017-11-08	827687	890823	807321	866073
# 8		2017-11-09	866073	890715	806285	806632
# 9		2017-11-10	806632	866885	751096	765882

列方向連結

new_df = pd.DataFrame([
    [-0.16], [0.19], [-0.24], [-1.51], [2.09], [-0.63],
    [1.51], [1.56], [3.78], [1.52], [1.16], [12.71],
    [3.81], [2.32], [-0.1], [0.93], [-1.86], [0.67],
    [0.1], [6.47], [0.41], [-1.32], [2.04], [-5.99],
    [2.26], [-0.76], [0.9], [0.12], [6.23], [-2.14],
    [5.68]
], columns=['ratio'])

df.join(new_df, how='inner')

# 		date		open	high	low		close	ratio
# 0		2017-10-01	488197	489003	489723	466627	-0.16
# 1		2017-10-02	489139	488197	493590	473007	0.19
# 2		2017-10-03	487987	489139	495358	478978	-0.24
# 3		2017-10-04	480618	487987	491477	472229	-1.51
# 4		2017-10-05	490673	480618	491379	468064	2.09
# 5		2017-10-06	487560	490673	496803	469707	-0.63
# 6		2017-10-07	494927	487560	495221	482922	1.51
# 7		2017-10-08	502668	494927	513768	489213	1.56
# 8		2017-10-09	521692	502668	539816	497525	3.78
# 9		2017-10-10	529605	521316	543653	509183	1.52
# 10	2017-10-11	535745	529605	579834	522659	1.16
# 11	2017-10-12	603830	535745	608320	526535	12.71
# 12	2017-10-13	626833	603830	679763	535514	3.81
# 13	2017-10-14	641376	626833	679763	579459	2.32
# 14	2017-10-15	640736	641376	649894	613526	-0.10
# 15	2017-10-16	646682	640736	657214	613343	0.93
# 16	2017-10-17	634682	646682	656551	628475	-1.86
# 17	2017-10-18	638945	634682	656831	597587	0.67
# 18	2017-10-19	639563	638945	652154	595464	0.10
# 19	2017-10-20	680935	639563	681990	634207	6.47
# 20	2017-10-21	683720	680935	705656	642202	0.41
# 21	2017-10-22	674718	683720	705573	666582	-1.32
# 22	2017-10-23	688504	674718	699988	657828	2.04
# 23	2017-10-24	647263	688504	717500	635624	-5.99
# 24	2017-10-25	661909	647263	675796	630626	2.26
# 25	2017-10-26	656882	661909	692724	617153	-0.76
# 26	2017-10-27	662773	672772	692997	652498	0.90
# 27	2017-10-28	663574	662773	681126	652498	0.12
# 28	2017-10-29	704915	663574	711090	653967	6.23
# 29	2017-10-30	689863	704915	718159	656695	-2.14
# 30	2017-10-31	729017	689863	733024	687718	5.68

型を指定して抽出

df.open.astype(float)

# 0    32631.0
# 1    32804.0
# 2    33104.0
# Name: open, dtype: float64

行列の反転

df.T

# 		0		1		2		3		4		5		6		7		8		9		...		21		22		23		24		25		26	27		28		29		30
# date	2017-10-01 00:00:00	2017-10-02 00:00:00	2017-10-03 00:00:00	2017-10-04 00:00:00	2017-10-05 00:00:00	2017-10-06 00:00:00	2017-10-07 00:00:00	2017-10-08 00:00:00	2017-10-09 00:00:00	2017-10-10 00:00:00	...	2017-10-22 00:00:00	2017-10-23 00:00:00	2017-10-24 00:00:00	2017-10-25 00:00:00	2017-10-26 00:00:00	2017-10-27 00:00:00	2017-10-28 00:00:00	2017-10-29 00:00:00	2017-10-30 00:00:00	2017-10-31 00:00:00
# open	488197	489139	487987	480618	490673	487560	494927	502668	521692	529605	...	674718	688504	647263	661909	656882	662773	663574	704915	689863	729017
# high	489003	488197	489139	487987	480618	490673	487560	494927	502668	521316	...	683720	674718	688504	647263	661909	672772	662773	663574	704915	689863
# low	489723	493590	495358	491477	491379	496803	495221	513768	539816	543653	...	705573	699988	717500	675796	692724	692997	681126	711090	718159	733024
# close	466627	473007	478978	472229	468064	469707	482922	489213	497525	509183	...	666582	657828	635624	630626	617153	652498	652498	653967	656695	687718
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?