1

I'm using python 2.7 and I have a dataframe with greek letters writen like this:

units = ['Ab_alpha_cd', 'XY_beta_zz', 'Ef_gamma_gh']
descriptions = ['Name1 (Ab_alpha_cd): description_1', 'Name2 (XY_beta_zz): description_2', 'Name3 (Ef_gamma_gh): description_3'
df = pd.dataframe
df['units'] = units
df['descriptions'] = descriptions

What I need is to change the '_greek_' in the descriptions column to the correct greek letters. I have tried several ways:

#first try:
for i in range (0, len(df)):
    df.loc[i,'descriptions'].replace('_alfa_','\u03B1').replace('_beta_','\u03B2').replace('_gamma_','\u03B3')

#second try:
for i in range (0, len(df)):
    df.loc[i,'descriptions'].replace('_alfa_',unichr(945)).replace('_beta_',unichr(946)).replace('_gamma_',unichr(947))

#thrid try
for i in range (0, len(df)):
    df.loc[i,'descriptions'].replace('_alfa_','α').replace('_beta_','β').replace('_gamma_','γ')

#fourth try
for i in range (0, len(df)):
    df.loc[i,'descriptions'].replace('_alfa_','α'.encode('utf-8')).replace('_beta_','β'.encode('utf-8')).replace('_gamma_','γ'.encode('utf-8'))

But nothing really worked. When I call df.head(), the greek letters do not show. They also do not show when I try wrinting the df to a csv file, even using utf-8 enconding.

I need the final result to be a list with the strings corrected.

What could I do?

Karol Duarte
  • 123
  • 1
  • 1
  • 8
  • 1
    What happened when you tried reading the documentation for `replace`? – Karl Knechtel Aug 06 '21 at 21:10
  • Welcome to stackoverflow, please read [tour] and [mre] and in this case also: [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) (1) – Andreas Aug 06 '21 at 22:23

1 Answers1

1

You can read the following in the documentation:

Dicts can be used to specify different replacement values for different existing values. For example, {'a': 'b', 'y': 'z'} replaces the value ‘a’ with ‘b’ and ‘y’ with ‘z’. To use a dict in this way the value parameter should be None.

Sample data:

import pandas as pd
df = pd.DataFrame({'A': ['Name1 (Ab_alpha_cd): description_1', 'Name2 (XY_beta_zz): description_2', 'Name3 (Ef_gamma_gh): description_3']})

Code:

df['A'].replace({'_alpha_':'α', '_beta_':'β', '_gamma_':'γ'}, regex=True)

Output:

                              A
0  Name1 (Abαcd): description_1
1  Name2 (XYβzz): description_2
2  Name3 (Efγgh): description_3
Andreas
  • 8,694
  • 3
  • 14
  • 38
  • from this solution, would a list obtained from this dictionary stay with the corrected greek letters? Sorry, I forgot to add in my question that the final result that I need is a list of strings. Just edited it. – Karol Duarte Aug 07 '21 at 13:46