如何用TSV文件中的选项卡替换逗号

时间:2015-06-19 18:39:25

标签: python pandas

在我的数据框中,我正在尝试使用制表符替换列curv_typ,maturity,bonds,geo\time中的逗号以及它下面的字符串中的逗号,以便我可以从中创建新列。

 curv_typ,maturity,bonds,geo\time  2015M06D16   2015M06D15   2015M06D11   \
0                 PYC_RT,Y1,GBAAA,EA        -0.24        -0.24        -0.24   
1               PYC_RT,Y1,GBA_AAA,EA        -0.02        -0.03        -0.10   
2                PYC_RT,Y10,GBAAA,EA         0.94         0.92         0.99   
3              PYC_RT,Y10,GBA_AAA,EA         1.67         1.70         1.60   
4                PYC_RT,Y11,GBAAA,EA         1.03         1.01         1.09 

代码如下,但它没有摆脱逗号,这就是我在努力的地方。

import os
import urllib2
import gzip
import StringIO
import pandas as pd

baseURL = "http://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file="
filename = "data/irt_euryld_d.tsv.gz"
outFilePath = filename.split('/')[1][:-3]

response = urllib2.urlopen(baseURL + filename)
compressedFile = StringIO.StringIO()
compressedFile.write(response.read())

compressedFile.seek(0)

decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb') 

with open(outFilePath, 'w') as outfile:
    outfile.write(decompressedFile.read())

#Now have to deal with tsv file
import csv

outFilePath = filename.split('/')[1][:-3] #As in the code above, just put here for reference
csvout = 'C:\Users\Sidney\ECB.tsv'
outfile = open(csvout, "w")
with open(outFilePath, "rb") as f:
    for line in f.read():
        line.replace(",", "\t")
        outfile.write(line)
outfile.close()

df = pd.DataFrame.from_csv("ECB.tsv", sep="\t", index_col=False)

谢谢

2 个答案:

答案 0 :(得分:2)

拆分列名以生成新列名,然后使用参数expand=True调用矢量化str split方法:

In [26]:
cols = 'curv_typ,maturity,bonds,geo\\time'.split(',')
df[cols] = df['curv_typ,maturity,bonds,geo\\time'].str.split(',', expand=True)
df

Out[26]:
  curv_typ,maturity,bonds,geo\time  2015M06D16  2015M06D15  2015M06D11  \
0               PYC_RT,Y1,GBAAA,EA       -0.24       -0.24       -0.24   
1             PYC_RT,Y1,GBA_AAA,EA       -0.02       -0.03       -0.10   
2              PYC_RT,Y10,GBAAA,EA        0.94        0.92        0.99   
3            PYC_RT,Y10,GBA_AAA,EA        1.67        1.70        1.60   
4              PYC_RT,Y11,GBAAA,EA        1.03        1.01        1.09   

  curv_typ maturity    bonds geo\time  
0   PYC_RT       Y1    GBAAA       EA  
1   PYC_RT       Y1  GBA_AAA       EA  
2   PYC_RT      Y10    GBAAA       EA  
3   PYC_RT      Y10  GBA_AAA       EA  
4   PYC_RT      Y11    GBAAA       EA  

修改

对于pandas版本0.16.0及更早版本,您需要使用以下代码:

df[cols] = df['curv_typ,maturity,bonds,geo\\time'].str.split(',').apply(pd.Series)

答案 1 :(得分:0)

我遇到了同样的问题。从欧洲统计局下载的数据具有相同的结构。我尝试了@ EdChum的解决方案,但我不能一步到位,所以这是我需要的进一步步骤:

vc.head() # The original DataFrame
Out[150]: 
  expend,unit,geo\time 2015    2014   2013   2012   2011    2010    2009   \
0       INV,MIO_EUR,AT  109    106.0   86.0  155.0  124.0   130.0   140.0   
1       INV,MIO_EUR,BE  722    664.0  925.0  522.0  590.0   476.0  1018.0   
2       INV,MIO_EUR,BG   16      1.0    2.0   65.0   11.0     5.0     6.0   
3       INV,MIO_EUR,CH  640   1237.0  609.0  662.0  640.0  1555.0   718.0   
4       INV,MIO_EUR,CZ   13     14.0   24.0   17.0  193.0    37.0    61.0   


cols = 'expend,unit,geo\time'.split(',') # Getting the columnns

clean = vc.iloc[:,0].str.split(',').apply(pd.Series) # Creating a clean version
clean = clean.rename(columns = lambda x: cols[x]) # Adding the column names to the clean version

vc = pd.concat([clean, vc.iloc[:,1:]], axis = 1) # Concatenating the two tables

vc.head()
Out[155]: 
  expend     unit geo\time 2015    2014   2013   2012   2011    2010    2009   \
0    INV  MIO_EUR       AT  109    106.0   86.0  155.0  124.0   130.0   140.0   
1    INV  MIO_EUR       BE  722    664.0  925.0  522.0  590.0   476.0  1018.0   
2    INV  MIO_EUR       BG   16      1.0    2.0   65.0   11.0     5.0     6.0   
3    INV  MIO_EUR       CH  640   1237.0  609.0  662.0  640.0  1555.0   718.0   
4    INV  MIO_EUR       CZ   13     14.0   24.0   17.0  193.0    37.0    61.0