Python Forum
Updating column name with translation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating column name with translation
#1
I have a column of countries. However mixed within this country column are spanish country names.
I have found the list of names in Spanish, translated them to english but now I want to either create another column showing the original column plus where my translate country will be or just replace the entry.

Quote:OriginalColumn of Countries
France
Portugal
Spain
México
Suiza
Perú
Alemania
Suecia
Reino Unido

I have my translated list

Quote:Mexico
Swiss
Peru
Germany
Sweden
United Kingdom
Now I want to put in another column and where i have found the translations in english, to insert them there (or if that fails, just update the original column with the translation so I should then have a list of all english countries

import pycountry import pandas as pd from pathlib import Path from langdetect import detect from googletrans import Translator FILENAME = r"POS.xlsx" COUNTRYNAME = 'Country' df = pd.read_excel(FILENAME) def all_names() -> set[str]: # all Country objects have a "name" attribute return {country.name for country in pycountry.countries} # type: ignore def all_official_names() -> set[str]: s: set[str] = set() for country in pycountry.countries: # not all Country objects have an "official_name" attribute try: s.add(country.official_name) # type: ignore except AttributeError: pass return s def get_df_countries(filename: Path) -> set[str]: # construct a set because country names may be duplicated in the spreadsheet column # this potentially improves runtime performance when parsing the country names later return set(pd.read_excel(FILENAME)[COUNTRYNAME]) # Function to detect language of a word def detect_language(word): try: return detect(word) except: return 'unknown' translator = Translator() if __name__ == "__main__": names = all_names() | all_official_names() for country in get_df_countries(Path(FILENAME)): status = "valid" if country in names else "invalid" if status =="invalid": #print(f"{country} is {status}") # Translate country back into english translated_country = (translator.translate(country).text) print(translated_country)

Any ideas how i can show either the new translations replaced but still show all others countries already in english?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking feedback: dynamic_i18n — automatic dynamic translation of HTML via OpenAI Bona2000 7 5,021 Sep-15-2025, 03:28 PM
Last Post: Bona2000
  Translation AliceCu 2 1,741 May-16-2023, 09:05 PM
Last Post: deanhystad
  Translation API snippyro 0 2,018 Nov-02-2021, 01:09 AM
Last Post: snippyro
  [GoogleTrans] How can i print my translation word ?... JamieVanCadsand 7 14,784 Aug-29-2021, 12:01 PM
Last Post: Melcu54
  Translation of R Code to Python for Statistical Learning Course SterlingAesir 2 3,256 Aug-27-2020, 08:46 AM
Last Post: ndc85430
  recording a translation table into a file arbiel 0 2,251 Mar-31-2020, 02:33 PM
Last Post: arbiel
  Manipulation a file for translation apsyrtos 1 2,920 Sep-10-2019, 02:56 AM
Last Post: luoheng
  Scilab -> Python translation silverfish 0 6,418 Mar-23-2018, 07:35 AM
Last Post: silverfish
  Python2 to Python3 translation - .keys() 20salmon 1 4,591 Nov-03-2017, 10:41 PM
Last Post: metulburr

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.