如何在pandas中使用一些条件语句来修改一列字符串(regex?)[英] How to modify a column of strings in pandas using a few conditionals (regex?)

本文是小编为大家收集整理的关于如何在pandas中使用一些条件语句来修改一列字符串(regex?)的处理方法,想解了如何在pandas中使用一些条件语句来修改一列字符串(regex?)的问题怎么解决?如何在pandas中使用一些条件语句来修改一列字符串(regex?)问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我有一个像这样的 df

    Identifier      Ticker      ISIN        Relationship Country .......
0   5.037663e+09    BTGGg.F     D10080162   Supplier     Germany .......
1   4.295870e+09    IVXG.DE     NaN         Supplier     Germany .......
2   5.043321e+09    SAPG.DE     D66992104   Customer     Germany
3   4.295869e+09    BMWG.DE     D12096109   Customer     Germany    
4   4.295870e+09    DTEGn.DE    D2035M136   Customer     Germany
5   4.295870e+09    IFXGn.DE    D35415104   Supplier     Germany
6   4.295869e+09    NSUG.DE     D04312100   Customer     Germany
7   5.000074e+09    EVKn.DE     D2R90Y117   Customer     Germany
8   4.295869e+09    LHAG.DE     D1908N106   Customer     Germany
9   4.295869e+09    MTXGn.DE    D5565H104   Supplier     Germany
10  4.295869e+09    SIEGn.DE    D69671218   Supplier     Germany
11  4.295870e+09    TKAG.DE     D8398Q119   Supplier     Germany
12  5.059963e+09    BNRGn.DE    D12459117   Customer     Germany
13  4.295869e+09    RHMG.DE     D65111102   Supplier     Germany
14  5.001195e+09    GBFG.DE     D11648108   Supplier     Germany
15  4.295869e+09    NXUG.DE     D5650J106   Customer     Germany
16  4.295870e+09    DPWGn.DE    D19225107   Supplier     Germany
17  4.295870e+09    ILM1k.DE    D22430116   Supplier     Germany
18  4.295869e+09    ADSGn.DE    D0066B185   Customer     Germany
19. 5.125125e+12    DBS.SG      D12300523.  Supplier     SG
........................................................................

在 df['Country'] == 'Germany' 的行中,

我有两个想要执行的功能.

功能1:我想隔离有小写字母的行,"."之前的任何小写字母,我想删除它,所以 BTGGg.F 将变为 BTGG.F,1LM1k.DE 将变为 1LM1.DE 但 NXUG.DE 不受影响.

在第一个函数之后使用新的数据框.

功能2:然后对于"."之前有大写 G 的行,我想删除 G,这样 RHMG.DE 将变为 RHM.DE,但 1LM1.DE 不受影响.

如果我只想删除"."之前的字母,函数会像 x = x.replace(x[x.find(".")-1],"") 一样简单.

但我不知道如何在数据框中执行此操作,也不知道如何应用我提到的条件.可以做到吗?如果可以,怎么做?

我在想它可能看起来像这样,但这显然行不通,我已经尝试过了.

df.loc[df['Country'].eq('Germany'),'Ticker'] = df.loc[df['Country'].eq('Germany'),'Ticker'].str.replace((df['Ticker'][df['Ticker'].find(".")-1],"") if df['Ticker'][df['Ticker'].find(".")-1] == '([a-z])')

紧随其后

df.loc[df['Country'].eq('Germany'),'Ticker'] = df.loc[df['Country'].eq('Germany'),'Ticker'].str.replace((df['Ticker'][df['Ticker'].find(".")-1],"") if df['Ticker'][df['Ticker'].find(".")-1] == 'G')

这是第一轮后输出的样子,来自 Country == Germany,删除"."前面的第一个小写字母.:

    Identifier      Ticker      ISIN        Relationship Country .......
0   5.037663e+09    BTGG.F      D10080162   Supplier     Germany .......
1   4.295870e+09    IVXG.DE     NaN         Supplier     Germany .......
2   5.043321e+09    SAPG.DE     D66992104   Customer     Germany
3   4.295869e+09    BMWG.DE     D12096109   Customer     Germany    
4   4.295870e+09    DTEG.DE     D2035M136   Customer     Germany
5   4.295870e+09    IFXG.DE     D35415104   Supplier     Germany
6   4.295869e+09    NSUG.DE     D04312100   Customer     Germany
7   5.000074e+09    EVK.DE      D2R90Y117   Customer     Germany
8   4.295869e+09    LHAG.DE     D1908N106   Customer     Germany
9   4.295869e+09    MTXG.DE     D5565H104   Supplier     Germany
10  4.295869e+09    SIEG.DE     D69671218   Supplier     Germany
11  4.295870e+09    TKAG.DE     D8398Q119   Supplier     Germany
12  5.059963e+09    BNRG.DE     D12459117   Customer     Germany
13  4.295869e+09    RHMG.DE     D65111102   Supplier     Germany
14  5.001195e+09    GBFG.DE     D11648108   Supplier     Germany
15  4.295869e+09    NXUG.DE     D5650J106   Customer     Germany
16  4.295870e+09    DPWG.DE     D19225107   Supplier     Germany
17  4.295870e+09    ILM1.DE     D22430116   Supplier     Germany
18  4.295869e+09    ADSG.DE     D0066B185   Customer     Germany
19. 5.125125e+12    DBS.SG      D12300523   Supplier     SG
........................................................................

这是在第二轮之后,来自 Country == Germany,删除"."之前的第一个大写"G":

    Identifier      Ticker      ISIN        Relationship Country .......
0   5.037663e+09    BTG.F       D10080162   Supplier     Germany .......
1   4.295870e+09    IVX.DE      NaN         Supplier     Germany .......
2   5.043321e+09    SAP.DE      D66992104   Customer     Germany
3   4.295869e+09    BMW.DE      D12096109   Customer     Germany    
4   4.295870e+09    DTE.DE      D2035M136   Customer     Germany
5   4.295870e+09    IFX.DE      D35415104   Supplier     Germany
6   4.295869e+09    NSU.DE      D04312100   Customer     Germany
7   5.000074e+09    EVK.DE      D2R90Y117   Customer     Germany
8   4.295869e+09    LHA.DE      D1908N106   Customer     Germany
9   4.295869e+09    MTX.DE      D5565H104   Supplier     Germany
10  4.295869e+09    SIE.DE      D69671218   Supplier     Germany
11  4.295870e+09    TKA.DE      D8398Q119   Supplier     Germany
12  5.059963e+09    BNR.DE      D12459117   Customer     Germany
13  4.295869e+09    RHM.DE      D65111102   Supplier     Germany
14  5.001195e+09    GBF.DE      D11648108   Supplier     Germany
15  4.295869e+09    NXU.DE      D5650J106   Customer     Germany
16  4.295870e+09    DPW.DE      D19225107   Supplier     Germany
17  4.295870e+09    ILM1.DE     D22430116   Supplier     Germany
18  4.295869e+09    ADS.DE      D0066B185   Customer     Germany
19. 5.125125e+12    DBS.SG      D12300523   Supplier     SG
........................................................................

推荐答案

df.loc[df.Country == 'Germany', 'Ticker'] = df[df.Country == 'Germany']['Ticker'].str.replace(r'[a-z](?=\.)', '').str.replace(r'G(?=\.)', '')
print(df)

打印:

      Identifier   Ticker        ISIN Relationship  Country
0   5.037663e+09    BTG.F   D10080162     Supplier  Germany
1   4.295870e+09   IVX.DE         NaN     Supplier  Germany
2   5.043321e+09   SAP.DE   D66992104     Customer  Germany
3   4.295869e+09   BMW.DE   D12096109     Customer  Germany
4   4.295870e+09   DTE.DE   D2035M136     Customer  Germany
5   4.295870e+09   IFX.DE   D35415104     Supplier  Germany
6   4.295869e+09   NSU.DE   D04312100     Customer  Germany
7   5.000074e+09   EVK.DE   D2R90Y117     Customer  Germany
8   4.295869e+09   LHA.DE   D1908N106     Customer  Germany
9   4.295869e+09   MTX.DE   D5565H104     Supplier  Germany
10  4.295869e+09   SIE.DE   D69671218     Supplier  Germany
11  4.295870e+09   TKA.DE   D8398Q119     Supplier  Germany
12  5.059963e+09   BNR.DE   D12459117     Customer  Germany
13  4.295869e+09   RHM.DE   D65111102     Supplier  Germany
14  5.001195e+09   GBF.DE   D11648108     Supplier  Germany
15  4.295869e+09   NXU.DE   D5650J106     Customer  Germany
16  4.295870e+09   DPW.DE   D19225107     Supplier  Germany
17  4.295870e+09  ILM1.DE   D22430116     Supplier  Germany
18  4.295869e+09   ADS.DE   D0066B185     Customer  Germany
19  5.125125e+12   DBS.SG  D12300523.     Supplier       SG

本文地址:https://www.itbaoku.cn/post/1727797.html