我正在尝试转换列的输出,该列当前如下所示:
0 Q122 Your Voice - Energy Group
1 Q122 Your Voice - Energy Group
2 Q122 Your Voice - BGSS
3 Q122 Your Voice - Strategy
4 Q122 Your Voice - Strategy
10265 Q122 Your Voice - Legal
10266 Q122 Your Voice - Legal
10267 Q122 Your Voice - Legal
10268 Q122 Your Voice - Legal
10269 Q122 Your Voice - Legal
Name: Survey Name, Length: 10270, dtype: object下面的代码检测第一个单词,并始终保留它,并相应地将其他任何内容替换为创建的字典。
下面的代码只生成输出的f{quarter}部分,但没有显示{replacements.get(key, '')}部分。
代码: print(df.loc:,“调查名称”)
replacements = {
"Your Voice - Energy Group": "Your Voice - E Group",
"Your Voice - BGSS": "Your Voice - Services & Solutions",
"Your Voice - Legal": "Your Voice - LRE",
"Your Voice - Strategy": "Your Voice - SGBR",
"Your Voice - Central Storage Line": "Your Voice - CSL"
}
def repl(match, repls=replacements):
quarter = match.group(1)
key = " ".join(match.group(2).strip().split())
return f"{quarter} {replacements.get(key, '')}"
res = df["Survey Name"].str.replace(r"(Q\d+)\s+(.+)", repl, regex=True)
print(res)产出如下:
该代码只生成输出的f{quarter}部分,但没有显示{replacements.get(key, '')}部分。
我得到的是:
0 Q122
1 Q122
2 Q122
3 Q122
4 Q122
10265 Q122
10266 Q122
10267 Q122
10268 Q122
10269 Q122
Name: Survey Name, Length: 10270, dtype: object我应该得到的是:
0 Q122 Your Voice - E Group
1 Q122 Your Voice - E Group
2 Q122 Your Voice - Services & Solutions
3 Q122 Your Voice - SGBR
4 Q122 Your Voice - SGBR
10265 Q122 Your Voice - LRECS
10266 Q122 Your Voice - LRECS
10267 Q122 Your Voice - LRECS
10268 Q122 Your Voice - LRECS
10269 Q122 Your Voice - LRECS
Name: Survey Name, Length: 10270, dtype: object什么会影响产出只给出四分之一的部分?
更新:
这是应用代码更改之前的原始输出。当我最初发布这个问题时,我故意改变了其中的一些内容。
0 Q122 Our Voice - BG Energy
1 Q122 Our Voice - BG Energy
2 Q122 Our Voice - BG Energy
3 Q122 Our Voice - BG Energy
4 Q122 Our Voice - BG Energy
10265 Q122 Our Voice - Corporate Affairs
10266 Q122 Our Voice - Corporate Affairs
10267 Q122 Our Voice - Corporate Affairs
10268 Q122 Our Voice - Corporate Affairs
10269 Q122 Our Voice - Corporate Affairs
Name: Survey Name, Length: 10270, dtype: object发布于 2022-07-28 12:13:11
使用这个repl函数来代替:
def repl(match, repls=replacements):
quarter = match.group(1)
key = " ".join(match.group(2).strip().split())
return f"{quarter} {replacements.get(key, key)}"请注意更改,现在行:
return f"{quarter} {replacements.get(key, '')}"是:
return f"{quarter} {replacements.get(key, key)}" # '' was changed by key输出
0 Q122 Our Voice - BG Energy
1 Q122 Our Voice - BG Energy
2 Q122 Our Voice - BG Energy
3 Q122 Our Voice - BG Energy
4 Q122 Our Voice - BG Energy
5 Q122 Our Voice - Corporate Affairs
6 Q122 Our Voice - Corporate Affairs
7 Q122 Our Voice - Corporate Affairs
8 Q122 Our Voice - Corporate Affairs
9 Q122 Our Voice - Corporate Affairs
10 Q122 Your Voice - LRE
11 Q122 Your Voice - LRE
12 Q122 Your Voice - LRE
13 Q122 Your Voice - LRE
14 Q122 Your Voice - LRE
Name: Survey Name, dtype: object输入
上面的输出是用这个DataFrame生成的:
{'Survey Name': {0: 'Q122 Our Voice - BG Energy',
1: 'Q122 Our Voice - BG Energy',
2: 'Q122 Our Voice - BG Energy',
3: 'Q122 Our Voice - BG Energy',
4: 'Q122 Our Voice - BG Energy',
5: 'Q122 Our Voice - Corporate Affairs',
6: 'Q122 Our Voice - Corporate Affairs',
7: 'Q122 Our Voice - Corporate Affairs',
8: 'Q122 Our Voice - Corporate Affairs',
9: 'Q122 Our Voice - Corporate Affairs',
10: 'Q122 Your Voice - Legal',
11: 'Q122 Your Voice - Legal',
12: 'Q122 Your Voice - Legal',
13: 'Q122 Your Voice - Legal',
14: 'Q122 Your Voice - Legal'}}https://stackoverflow.com/questions/73151952
复制相似问题