如何在TSV-Fi中用制表符替换逗号

2024-05-06 12:31:51 发布

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

在下面的dataframe中,我试图用制表符替换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 

代码如下所示,但它没有去掉逗号,这是我正在努力的地方。在

^{pr2}$

谢谢你


Tags: timegeopyc逗号rteaaaay1
2条回答

拆分列名以生成新的列名,然后使用param^{cd3>}调用向量化的^{}^{}方法:

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及更早版本,则需要使用以下行:

^{pr2}$

我也有同样的问题。从欧盟统计局下载的数据,其结构相同。我尝试了@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   

相关问题 更多 >