'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 1:[1]
Use np where(), checking if language has an alphanumeric or not.
df['language']=np.where(df['language'].str.contains('\w'),df['language'],'N/A')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wwnde |
