【问题标题】:Insert string in parentheses of a column at the beginning of a string of another column python在另一列python的字符串开头的列的括号中插入字符串
【发布时间】:2020-01-06 16:35:26
【问题描述】:

我有一个如下所示的数据框:

Name    Country         (Title)
0  Elizabeth    England      (Queen of)
1    Jackson        Pop       (King of)
2      Trump        USA  (President of)
3     Macron     France  (President of)
4    Clinton        USA            None
5      Blair    England            None
6      Elvis  RocknRoll       (King of)

我想得到的是以下内容:

 Name             Country                (Title)
0  Elizabeth       Queen of England      (Queen of)
1    Jackson       King of Pop           (King of)
2      Trump       President of USA      (President of)
3     Macron       President of France   (President of)
4    Clinton       USA                   None
5      Blair       England               None
6      Elvis       King of RocknRoll     (King of)

我已经做了几次尝试(包括使用正则表达式)。我已经尝试了下面的代码,它非常清晰但也不起作用。

欢迎提出任何建议!

谢谢。

def extract(w):
    begin = w.find('(')
    if begin == -1:
        return ''
    begin += 1 
    end = w.find(')', begin)
    if end == -1: 
        return w[begin:]
    else:
        return w[begin:end]
for i in range(len(table)):
    if table['(Title)'][i] != None :
        table['Country'][i] = extract(table['Title'[i]).str.cat(table['Country'].values.astype(str), sep='')
 Name             Country                (Title)
0  Elizabeth       Queen of England      (Queen of)
1    Jackson       King of Pop           (King of)
2      Trump       President of USA      (President of)
3     Macron       President of France   (President of)
4    Clinton       USA                   None
5      Blair       England               None
6      Elvis       King of RocknRoll     (King of)

【问题讨论】:

    标签: python insert word parentheses


    【解决方案1】:

    你可以用这个:

    df['new_country'] = df['(title)'].str[1:-1] + ' ' + df['country']
    df.loc[pd.notna(df['(title)']), 'country'] = df['new_country']
    

    【讨论】:

    • 谢谢亚瑞兹。即使它们不是括号,您的代码也会删除第一个和最后一个字符串。我会努力解决这个问题。
    • 这是我找到的代码:for i in range(len(table)): if table['Title'][i] != None : if table['Title'][i] .startswith('('): table['New'][i] = table['Title'][i][1:-1] + ' ' + table['Country'][i] else : table[ 'New'][i] = table['Title'][i] + '' + table['Country'][i] else : table['New'][i] = None table
    • @levesxv 循环遍历 pandas 数据框中的行非常低效。在这种情况下,他的数据似乎是用括号组织的,所以这是一个合适的解决方案。在其他情况下,一个好的解决方案是df['(title)'].str.replace('(', '').str.replace(')', ''),但可能使用正则表达式会更聪明。
    • 感谢您的建议。数据集包括带括号的数据和不带括号的数据(如我的示例所示)。您建议的最后一段代码确实更好。我对正则表达式并不完全满意(并且熟悉)。任何帮助或建议将不胜感激。谢谢!
    【解决方案2】:

    解决方案:-

    for idx, row in df.iterrows():
        concat_string = row.Title.lstrip('(').rstrip(')')+' '+ row.Country if row.Title else row.Country
    
        df.at[idx, 'Country'] = concat_string
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多