2017-02-09 147 views
-1

我想分割我的txt文件我有';'分成熊貓數據框。使用pd.series將csv拆分爲多列

價格指數 - 歐洲貨幣

Date ;Blue-Chip;Blue-Chip;Broad ; Broad ;Ex UK ;Ex Euro Zone;Blue-Chip; Broad 
     ; Europe ;Euro-Zone;Europe ;Euro-Zone;   ;   ; Nordic ; Nordic 
     ; SX5P ; SX5E ;SXXP  ;SXXE  ; SXXF ; SXXA ; DK5F ; DKXF 
31.12.1986;775.00 ; 900.82 ; 82.76 ; 98.58 ; 98.06 ; 69.06 ; 645.26 ; 65.56 
01.01.1987;775.00 ; 900.82 ; 82.76 ; 98.58 ; 98.06 ; 69.06 ; 645.26 ; 65.56 
02.01.1987;770.89 ; 891.78 ; 82.57 ; 97.80 ; 97.43 ; 69.37 ; 647.62 ; 65.81 
05.01.1987;771.89 ; 898.33 ; 82.82 ; 98.60 ; 98.19 ; 69.16 ; 649.94 ; 65.82 
06.01.1987;775.92 ; 902.32 ; 83.28 ; 99.19 ; 98.83 ; 69.50 ; 652.49 ; 66.06 
07.01.1987;781.21 ; 899.15 ; 83.78 ; 98.96 ; 98.62 ; 70.59 ; 651.97 ; 66.20 
08.01.1987;777.62 ; 887.37 ; 83.52 ; 97.87 ; 97.68 ; 71.01 ; 645.57 ; 65.62 
09.01.1987;769.80 ; 868.31 ; 83.03 ; 96.31 ; 96.22 ; 71.40 ; 638.03 ; 65.14 
12.01.1987;775.07 ; 879.41 ; 83.64 ; 97.54 ; 97.18 ; 71.50 ; 634.14 ; 65.03 
13.01.1987;770.00 ; 872.74 ; 83.00 ; 96.78 ; 96.38 ; 70.97 ; 622.44 ; 63.87 
14.01.1987;772.04 ; 876.39 ; 82.99 ; 97.14 ; 96.59 ; 70.66 ; 603.63 ; 62.46 
15.01.1987;779.12 ; 884.37 ; 83.77 ; 98.10 ; 97.60 ; 71.28 ; 620.01 ; 63.89 
16.01.1987;781.66 ; 883.78 ; 84.15 ; 98.11 ; 97.66 ; 71.95 ; 623.77 ; 64.65 

完整的數據集可以從以下網址

https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt

檢索我文件讀入到使用下面的代碼大熊貓。

data=pd.read_csv(txt,encoding='utf8') 

我得到一個n乘1的數據框,現在我需要分隔列。我以爲我可以放棄前三行將列分割爲「;」然後再添加標題。我正在嘗試使用以下功能。

data1=pd.Series.str.split(data,pat=';',expand=True) 

和這個返回

TypeError: len() of unsized object 

我試圖N = 9應該有9列,但這返回相同的錯誤消息。

data1=pd.Series.str.split(data,pat=';',n=9, expand=True) 

我也試過這個。

data1 = pd.read_csv(txt,index_col=0,parse_dates=True,sep";",dayfirst=True) 

但這返回錯誤

EmptyDataError: No columns to parse from file 
+1

請不要張貼您的數據的圖像;張貼我們實際上可以複製粘貼的東西... – blacksite

+1

你得到了什麼輸出?你期望輸出什麼? – 2017-02-09 22:09:46

回答

1

這是你想要的嗎?

import pandas as pd 
import io 
import requests 

url = 'https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt' 

r = requests.get(url) 

df = pd.read_csv(io.StringIO(r.text.replace(';\n', '\n')), 
       sep='\s*;\s*', 
       engine='python', 
       skiprows=1, 
       header=[0,1,2], 
       index_col=0, 
       parse_dates=True, 
       dayfirst=True) 

結果:

In [266]: df.head() 
Out[266]: 
Date  Blue-Chip   Broad      Ex UK  Ex Euro Zone Blue-Chip Broad 
       Europe Euro-Zone Europe Euro-Zone Unnamed: 5_level_1 Unnamed: 6_level_1 Nordic Nordic 
       SX5P  SX5E SXXP  SXXE    SXXF    SXXA  DK5F DKXF 
1986-12-31 775.00 900.82 82.76  98.58    98.06    69.06 645.26 65.56 
1987-01-01 775.00 900.82 82.76  98.58    98.06    69.06 645.26 65.56 
1987-01-02 770.89 891.78 82.57  97.80    97.43    69.37 647.62 65.81 
1987-01-05 771.89 898.33 82.82  98.60    98.19    69.16 649.94 65.82 
1987-01-06 775.92 902.32 83.28  99.19    98.83    69.50 652.49 66.06 

In [267]: df.shape 
Out[267]: (7673, 8) 
+0

是的,這適用於我在上面複製的txt文件的片段,但是當我在整個文件中嘗試它時,我收到錯誤消息「Index Error:List index out of range」 – FutureQuant

+1

@FutureQuant,請發佈__reproducible__數據集 – MaxU

+0

道歉我已經在最初的帖子 – FutureQuant