使用pd系列将csv拆分为多列

2024-05-29 05:09:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将我的txt文件“;”分割成一个数据帧。你知道吗

价格指数-欧元

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

可以从以下url检索完整的数据集

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

我使用以下代码将文件读入pandas。你知道吗

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

Tags: 文件数据txttruezonedatablueex
1条回答
网友
1楼 · 发布于 2024-05-29 05:09:03

这就是你想要的吗?你知道吗

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)

相关问题 更多 >

    热门问题