根据来自另一列中的字符串值的多个条件添加后缀
add suffix based on multiple conditions from string values in another column
当在另一列中满足条件时,我想为一列中的字符串添加后缀。如果“市场”列中存在一个值,“符号”列对应的值将更新为包含当前代码,但我想为其添加一个后缀以表示其市场位置。我想我可以创建多个掩码并为每个值使用多行代码更改多个值,但我想知道是否存在在一次操作中执行此操作的更优雅的方法。
这是我试过的:
conditions = [ (df['Market'].str.contains("Oslo")), (df['Market'].str.contains("Paris")), (df['Market'].str.contains("Amsterdam")), (df['Market'].str.contains("Brussels")), (df['Market'].str.contains("Dublin")) ] values = [str+'.OL', str+'.PA', str+'.AS', str+'.BR', str+'.IR'] df['Symbol'] = np.select(conditions, values) print(df)
我收到一个错误:
unsupported operand type(s) for +: 'type' and 'str'
欢迎任何帮助
在 KingOtto 的回答后添加...
数据框:
| Name | Symbol | Market |
|:------------------|:---------|:-----------------------|
| 1000MERCIS | ALMIL | Euronext Growth Paris |
| 2020 BULKERS | 2020 | Oslo Børs |
| 2CRSI | 2CRSI | Euronext Paris |
| 2MX ORGANIC | 2MX | Euronext Paris |
| 2MX ORGANIC BS | 2MXBS | Euronext Paris |
| 5TH PLANET GAMES | 5PG | Euronext Expand Oslo |
| A TOUTE VITESSE | MLATV | Euronext Access Paris |
| A.S.T. GROUPE | ASP | Euronext Paris |
| AALBERTS NV | AALB | Euronext Amsterdam |
| AASEN SPAREBANK | AASB | Euronext Growth Oslo |
| AB INBEV | ABI | Euronext Brussels |
| AB SCIENCE | AB | Euronext Paris |
| ABATTOIR | - | Euronext Expert Market |
| ABC ARBITRAGE | ABCA | Euronext Paris |
| ABEO | ABEO | Euronext Paris |
创建一个带有后缀列的新df,然后将这些值与符号列合并以获得雅虎财经正确理解的代码以供查询。
suffix_list = pd.DataFrame({'Market': ['Euronext Growth Oslo','Euronext Expand Oslo', 'Oslo Børs', 'Euronext Brussels','Euronext Growth Brussels','Euronext Paris', 'Euronext Access Paris','Euronext Growth Paris', 'Euronext Lisbon', 'Euronext Access Lisbon', 'Euronext Dublin', 'Euronext Growth Dublin', 'Euronext Brussels', 'Euronext Access Brussels','Euronext Amsterdam'], 'suffix':['.OL','.OL','.OL','.BR','.BR','.PA','.PA','.PA','.LI','.LI','.IR','.IR','.BR','.BR','.AS']})
new_df = pd.merge(df, suffix_list, how='left', on='Market')
new_df['Ticker']=new_df['Symbol']+new_df['suffix']
包含 Ticker 列的新数据框:
| Name | Symbol | Market | suffix | Ticker |
| :-----------------|:---------|:-----------------------|:---------|:---------|
| 1000MERCIS | ALMIL | Euronext Growth Paris | .PA | ALMIL.PA |
| 2020 BULKERS | 2020 | Oslo Børs | .OL | 2020.OL |
| 2CRSI | 2CRSI | Euronext Paris | .PA | 2CRSI.PA |
| 2MX ORGANIC | 2MX | Euronext Paris | .PA | 2MX.PA |
| 2MX ORGANIC BS | 2MXBS | Euronext Paris | .PA | 2MXBS.PA |
| 5TH PLANET GAMES | 5PG | Euronext Expand Oslo | .OL | 5PG.OL |
| A TOUTE VITESSE | MLATV | Euronext Access Paris | .PA | MLATV.PA |
| A.S.T. GROUPE | ASP | Euronext Paris | .PA | ASP.PA |
| AALBERTS NV | AALB | Euronext Amsterdam | .AS | AALB.AS |
| AASEN SPAREBANK | AASB | Euronext Growth Oslo | .OL | AASB.OL |
| AB INBEV | ABI | Euronext Brussels | .BR | ABI.BR |
| AB INBEV | ABI | Euronext Brussels | .BR | ABI.BR |
| AB SCIENCE | AB | Euronext Paris | .PA | AB.PA |
| ABATTOIR | - | Euronext Expert Market | nan | nan |
| ABC ARBITRAGE | ABCA | Euronext Paris | .PA | ABCA.PA |
| ABEO | ABEO | Euronext Paris | .PA | ABEO.PA |
您需要分 3 个步骤进行
您需要定义一个详尽无遗的suffix_list - 一个只为每个市场保存一次信息的字典
suffix_list = pd.DataFrame({'Market': ['Oslo', 'Paris'], 'suffix':['OL','PA']})
您想将 suffix_list
作为一个新列合并到您现有的数据框中 - 所有市场的一个命令(对于列表中有后缀的每个市场,您添加该后缀):
pd.merge(df, suffix_list, how='left', on='Market')
现在所有行的 'value'
和 'suffix'
两列彼此相邻,您可以对所有行应用 1 个操作
str('value')+'suffix'
当在另一列中满足条件时,我想为一列中的字符串添加后缀。如果“市场”列中存在一个值,“符号”列对应的值将更新为包含当前代码,但我想为其添加一个后缀以表示其市场位置。我想我可以创建多个掩码并为每个值使用多行代码更改多个值,但我想知道是否存在在一次操作中执行此操作的更优雅的方法。
这是我试过的:
conditions = [ (df['Market'].str.contains("Oslo")), (df['Market'].str.contains("Paris")), (df['Market'].str.contains("Amsterdam")), (df['Market'].str.contains("Brussels")), (df['Market'].str.contains("Dublin")) ] values = [str+'.OL', str+'.PA', str+'.AS', str+'.BR', str+'.IR'] df['Symbol'] = np.select(conditions, values) print(df)
我收到一个错误:
unsupported operand type(s) for +: 'type' and 'str'
欢迎任何帮助
在 KingOtto 的回答后添加...
数据框:
| Name | Symbol | Market |
|:------------------|:---------|:-----------------------|
| 1000MERCIS | ALMIL | Euronext Growth Paris |
| 2020 BULKERS | 2020 | Oslo Børs |
| 2CRSI | 2CRSI | Euronext Paris |
| 2MX ORGANIC | 2MX | Euronext Paris |
| 2MX ORGANIC BS | 2MXBS | Euronext Paris |
| 5TH PLANET GAMES | 5PG | Euronext Expand Oslo |
| A TOUTE VITESSE | MLATV | Euronext Access Paris |
| A.S.T. GROUPE | ASP | Euronext Paris |
| AALBERTS NV | AALB | Euronext Amsterdam |
| AASEN SPAREBANK | AASB | Euronext Growth Oslo |
| AB INBEV | ABI | Euronext Brussels |
| AB SCIENCE | AB | Euronext Paris |
| ABATTOIR | - | Euronext Expert Market |
| ABC ARBITRAGE | ABCA | Euronext Paris |
| ABEO | ABEO | Euronext Paris |
创建一个带有后缀列的新df,然后将这些值与符号列合并以获得雅虎财经正确理解的代码以供查询。
suffix_list = pd.DataFrame({'Market': ['Euronext Growth Oslo','Euronext Expand Oslo', 'Oslo Børs', 'Euronext Brussels','Euronext Growth Brussels','Euronext Paris', 'Euronext Access Paris','Euronext Growth Paris', 'Euronext Lisbon', 'Euronext Access Lisbon', 'Euronext Dublin', 'Euronext Growth Dublin', 'Euronext Brussels', 'Euronext Access Brussels','Euronext Amsterdam'], 'suffix':['.OL','.OL','.OL','.BR','.BR','.PA','.PA','.PA','.LI','.LI','.IR','.IR','.BR','.BR','.AS']})
new_df = pd.merge(df, suffix_list, how='left', on='Market')
new_df['Ticker']=new_df['Symbol']+new_df['suffix']
包含 Ticker 列的新数据框:
| Name | Symbol | Market | suffix | Ticker |
| :-----------------|:---------|:-----------------------|:---------|:---------|
| 1000MERCIS | ALMIL | Euronext Growth Paris | .PA | ALMIL.PA |
| 2020 BULKERS | 2020 | Oslo Børs | .OL | 2020.OL |
| 2CRSI | 2CRSI | Euronext Paris | .PA | 2CRSI.PA |
| 2MX ORGANIC | 2MX | Euronext Paris | .PA | 2MX.PA |
| 2MX ORGANIC BS | 2MXBS | Euronext Paris | .PA | 2MXBS.PA |
| 5TH PLANET GAMES | 5PG | Euronext Expand Oslo | .OL | 5PG.OL |
| A TOUTE VITESSE | MLATV | Euronext Access Paris | .PA | MLATV.PA |
| A.S.T. GROUPE | ASP | Euronext Paris | .PA | ASP.PA |
| AALBERTS NV | AALB | Euronext Amsterdam | .AS | AALB.AS |
| AASEN SPAREBANK | AASB | Euronext Growth Oslo | .OL | AASB.OL |
| AB INBEV | ABI | Euronext Brussels | .BR | ABI.BR |
| AB INBEV | ABI | Euronext Brussels | .BR | ABI.BR |
| AB SCIENCE | AB | Euronext Paris | .PA | AB.PA |
| ABATTOIR | - | Euronext Expert Market | nan | nan |
| ABC ARBITRAGE | ABCA | Euronext Paris | .PA | ABCA.PA |
| ABEO | ABEO | Euronext Paris | .PA | ABEO.PA |
您需要分 3 个步骤进行
您需要定义一个详尽无遗的suffix_list - 一个只为每个市场保存一次信息的字典
suffix_list = pd.DataFrame({'Market': ['Oslo', 'Paris'], 'suffix':['OL','PA']})
您想将
suffix_list
作为一个新列合并到您现有的数据框中 - 所有市场的一个命令(对于列表中有后缀的每个市场,您添加该后缀):pd.merge(df, suffix_list, how='left', on='Market')
现在所有行的
'value'
和'suffix'
两列彼此相邻,您可以对所有行应用 1 个操作str('value')+'suffix'