1. 准备数据
import pandas as pd
from io import StringIO
csv_txt = '''"date","player1","player2","score1","score2"
"2017-06-05","张继科","林思远",3,2
"2017-06-06","丁宁","刘思文",3,0
"2017-06-07","马琳","樊振东",2,3
"2017-06-08","张燕","丁宁",0,3
"2017-06-09","张继科","马琳",3,2
"2017-06-10","刘思文","张燕",4,1
"2017-06-11","马琳","林思远",3,2
'''
#df = pd.read_csv(StringIO(csv_txt), header=0, index_col="date") # 以 date 作为 index
df = pd.read_csv(StringIO(csv_txt), header=0) # 默认 index
2. 列 ——> 索引
df.set_index('date', inplace=True) # column 改为 index
3. 索引 ——> 列
df['index'] = df.index
df.reset_index(level=0, inplace=True)
df.reset_index(level=['tick', 'obs'])
-
df['si_name'] = df.index.get_level_values('si_name')
# where si_name is the name of the subindex.
df.reset_index() # (all)index 改为 column
#df.reset_index(level=0, inplace=True) # (the first)index 改为 column