Search code examples
pandasreplacefillna

Filling blanks in a pandas dataframe column leads to reversal of function


I have the following dataframe:

   a    b      x  y    language
0  id1  id_2   3 
1  id2  id_4   6 ,0=/%
2  id3  id_6   9 |-|/#
3  id4  id_8  12 text4

I used langdetect to detect the language of the text elements in column y.

This is the code I have used for that purpose:

for i, row in df.iterrows():
try:
    df.loc[i, "language"] = detect(row["y"])
except:
    continue

This is the result:

   a    b      x  y    language
0  id1  id_2   3 
1  id2  id_4   6 ,0=/%
2  id3  id_6   9 |-|/#
3  id4  id_8  12 text4  en
4  id5  id_9  14 text5  de
5  id6  id_10 12 

I then attempted to fill the blanks in the language column with the string "N/A" using any one of the following commands:

df['language'].replace([''],"N/A", inplace=True)

df['language'] = df['language'].fillna(0)

For each command above, I received the following results:

      a    b    x  y    language
 0  id1  id_2   3 N/A   N/A
 1  id2  id_4   6 ,0=/% ,0=/%
 2  id3  id_6   9 |-|/# |-|/#
 3  id4  id_8  12 text4 text4  
 4  id5  id_9  14 text5 text5 
 5  id6  id_10 12 N/A   N/A

How do I get the following result:

   a    b      x  y    language
0  id1  id_2   3        N/A
1  id2  id_4   6 ,0=/%  N/A
2  id3  id_6   9 |-|/#  N/A
3  id4  id_8  12 text4  en
4  id5  id_9  14 text5  de
5  id6  id_10 12        N/A

Solution

  • this works!

    initial dataframe:

       a    b     x   y language new
    0  0  id1  id_2   3     None    
    1  1  id2  id_4   6    ,0=/%    
    2  2  id3  id_6   9           kl
    3  3  id4  id_8  12    text4    
    

    used replace just use space

    df.new=df['new'].replace(" ",'n/a')#or
    
    df['new'].replace(" ",'n/a',inplace=True)#also works
    

    output:

       a    b     x   y language  new
    0  0  id1  id_2   3     None  n/a
    1  1  id2  id_4   6    ,0=/%  n/a
    2  2  id3  id_6   9            kl
    3  3  id4  id_8  12    text4  n/a