Last active
December 14, 2017 08:43
-
-
Save kyoro1/8d906db5248399328ad488ce2777f5db to your computer and use it in GitHub Desktop.
indexからcolumnへ、columnからindexへ ref: https://qiita.com/kyoro1/items/56c1606e0218fbfae32d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = \ | |
pd.DataFrame({ 'date' : ['2017-12-14','2017-12-15','2017-12-16'], | |
'symbol' : ["A","B","C"], | |
'number' : [1,2,3]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> df | |
date number symbol | |
0 2017-12-14 1 A | |
1 2017-12-15 2 B | |
2 2017-12-16 3 C | |
>>> df.index | |
RangeIndex(start=0, stop=3, step=1) | |
>>> df.columns | |
Index([u'date', u'number', u'symbol'], dtype='object') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## "date"カラムの内容をindexに指定 | |
df1 = df.set_index("date") | |
>>> df1 | |
number symbol | |
date | |
2017-12-14 1 A | |
2017-12-15 2 B | |
2017-12-16 3 C | |
>>> df1.index | |
Index([u'2017-12-14', u'2017-12-15', u'2017-12-16'], dtype='object', name=u'date') | |
>>> df1.columns | |
Index([u'number', u'symbol'], dtype='object') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df2 = df1.reset_index() | |
>>> df2 | |
date number symbol | |
0 2017-12-14 1 A | |
1 2017-12-15 2 B | |
2 2017-12-16 3 C | |
>>> df2.index | |
RangeIndex(start=0, stop=3, step=1) | |
>>> df2.columns | |
Index([u'date', u'number', u'symbol'], dtype='object') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment