I am trying to replace multiple string values in a column and I understand that I can use the replace() to do it one by one. Given I need to replace more than 10 string values, I am just wondering if there's a faster way to replace a number of string values to the same value.
df = pd.DataFrame({'a':["US", "Japan", "UK", "China", "Peru", "Germany"]})
df.replace({'a' : { 'Japan' : 'Germany', 'UK' : 'Germany', 'China' : 'Germany' }})
Expected output:
a
0 US
1 Germany
2 Germany
3 Germany
4 Peru
5 Germany
df.replace('Japan|UK|China', 'Germany', regex=True). Thedf.replace()can handel regualr expressions, there you can combine multiple strings/groups.