我的数据是
Date Code Buying_Date Buying_Price
20140328 2920 20140211 4290
20140328 3200 20140218 122500
20140328 43370 20140328 21500
20140331 8730 20140204 12150
20140331 13030 20140205 28100
20140331 15750 20140319 13336
20140331 107590 20140324 32889
20140331 3090 20140327 7894
20140331 17390 20140327 123500
20140331 43370 20140328 21500
20140401 8730 20140204 12150
20140401 13030 20140205 28100
20140401 4100 20140205 715
我的愿望结果是
“日期”和“代码”列更改为多重索引和数据集,如下所示
Date Code Buying_Date Buying_Price
20140328 2920 20140211 4290
3200 20140218 122500
43370 20140328 21500
20140331 8730 20140204 12150
13030 20140205 28100
15750 20140319 13336
107590 20140324 32889
3090 20140327 7894
17390 20140327 123500
43370 20140328 21500
20140401 8730 20140204 12150
13030 20140205 28100
4100 20140205 715
谢谢您的建议。
答案 0 :(得分:0)
这应该做到:
import sys
import pandas as pd
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
data = StringIO("""20140328 2920 20140211 4290
20140328 3200 20140218 122500
20140328 43370 20140328 21500
20140331 8730 20140204 12150
20140331 13030 20140205 28100
20140331 15750 20140319 13336
20140331 107590 20140324 32889
20140331 3090 20140327 7894
20140331 17390 20140327 123500
20140331 43370 20140328 21500
20140401 8730 20140204 12150
20140401 13030 20140205 28100
20140401 4100 20140205 715""")
df = pd.read_csv(data, sep=" ", names=['Date', 'Code', 'Buying_Date', 'Buying_Price'])
df.set_index(['Date','Code'])