使用Python的多个子字符串过滤器的建议

时间:2019-03-15 12:49:07

标签: python-3.x string pandas multiple-columns

BUILD ERROR
: Can't resolve all parameters for ɵb in B:/0. Projects/dashboard/lib-shared/node_modules/@myOrg/core/core.d.ts: (?, [object Object], [object Object]).

enter image description here

或:

My Dataframe:

有人建议如何克服这个问题吗?

在第1列中,首先过滤出“ XSWX ”和“ XVTX ”以获取“ SW ”和“ VX ”。

...然后在第2列中仅将' .S '字符串替换为' SW '和' VX '

...获得显示“ AAM.SW ”和“ ABBN.VX ”的结果,而不是其当前的“ AAM.S ” ”和“ AABN.S ”。

2 个答案:

答案 0 :(得分:1)

您要使用np.select,因为您有多个条件。因此,我们可以修复我们的reuters_exchange_code列。之后,我们将Symbol List换成bats_name来填充reuters_exchange_code,如下所示:

conditions = [
    (df['mic'] == 'XSWX'),
    (df['mic'] == 'XVTX')
]

choices = ['.SW','.VX']

df['reuters_exchange_code'] = np.select(conditions, 
                                        choices, 
                                        default=df['reuters_exchange_code'])

df['Symbol List'] = df['bats_name'] + df['reuters_exchange_code']

print(df)
   Symbol List bats_name                                  company_name   mic  \
0      1COV.DE      1COV                                   Covestro AG  XETR   
1       2GB.DE       2GB                                   G Energy AG  XETR   
2      2HRA.DE      2HRA                            H&R GmbH & Co KGaA  XETR   
3        3IN.L       3IN                         3i Infrastructure PLC  XLON   
4        888.L       888                              888 Holdings PLC  XLON   
5       93M.DE       93M                            MPH Health Care AG  XETR   
6      A1OS.DE      A1OS                          All for One Steeb AG  XETR   
7       A2A.MI       A2A                                       A2A SpA  MTAA   
8       A3M.MC       A3M  Atresmedia Corp de Medios de Comunicacion SA  XMAD   
9       A4Y.DE       A4Y                       Accentro Real Estate AG  XETR   
10       A5G.I       A5G                                 AIB Group PLC  XDUB   
11      AAB.CO       AAB                      Aalborg Boldspilklub A/S  XCSE   
12      AAD.DE       AAD                               Amadeus Fire AG  XETR   
13      AAG.DE       AAG                                     Aumann AG  XETR   
14      AAK.ST       AAK                                        AAK AB  XSTO   
15     AALB.AS      AALB                        Aalberts Industries NV  XAMS   
16       AAL.L       AAL                            Anglo American PLC  XLON   
17      AAM.SW       AAM                            Anglo American PLC  XSWX   
18      AAQ.DE       AAQ                             AAP Implantate AG  XETR   
19       AAS.L       AAS              Aberdeen Standard Asia Focus PLC  XLON   
20        AA.L        AA                                        AA PLC  XLON   
21     ABBN.VX      ABBN                                       ABB Ltd  XVTX   
22      ABB.MC       ABB                                 AB-Biotics SA  XMAD   
23      ABB.ST       ABB                                       ABB Ltd  XSTO   
24     ABCA.PA      ABCA                                 ABC arbitrage  XPAR   
25     ABEO.PA      ABEO                                       Abeo SA  XPAR   
26       ABF.L       ABF                  Associated British Foods PLC  XLON   
27     ABGP.MC      ABGP                                    Abengoa SA  XMAD   
28      ABG.MC       ABG                                    Abengoa SA  XMAD   
29     ABIO.PA      ABIO                                    Albioma SA  XPAR   

   reuters_exchange_code  
0                    .DE  
1                    .DE  
2                    .DE  
3                     .L  
4                     .L  
5                    .DE  
6                    .DE  
7                    .MI  
8                    .MC  
9                    .DE  
10                    .I  
11                   .CO  
12                   .DE  
13                   .DE  
14                   .ST  
15                   .AS  
16                    .L  
17                   .SW  
18                   .DE  
19                    .L  
20                    .L  
21                   .VX  
22                   .MC  
23                   .ST  
24                   .PA  
25                   .PA  
26                    .L  
27                   .MC  
28                   .MC  
29                   .PA  

让过滤器仅对已更改以显示其行的行进行过滤:

print(df[(df['mic'] == 'XSWX') | (df['mic'] == 'XVTX')][['Symbol List', 'mic', 'reuters_exchange_code']])

   Symbol List   mic reuters_exchange_code
17      AAM.SW  XSWX                   .SW
21     ABBN.VX  XVTX                   .VX

答案 1 :(得分:0)

如果我明白你想要什么:

#modify the code .S to SW for XSWX
df.loc[df['reuters_exchange_code'].eq('.S') & df['mic'].eq('XSWX'), 'reuters_exchange_code'] = 'SW'
#modify the code .S to VX for XVTX
df.loc[df['reuters_exchange_code'].eq('.S') & df['mic'].eq('XVTX'), 'reuters_exchange_code'] = 'VX'
:
:
#rebuild the symbol list
df['Symbol List'] = df['bats_name'] + df['reuters_exchange_code']
df = df.set_index('Symbol List')