Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: convert unicode strings to string

Tags:

python

pandas

I have data frame and there are 2 columns with unicode value. I need to convert it to string. I try df.domain.astype(str) but it return unicode strings. How can I do that? Data looks like (I need to convert either columns)

domain       search_term
vk.com           None
facebook.com     None
yandex.ru        снять квартиру
locals.ru        None
yandex.ru        снять квартиру без посредников в Москве
avito.ru         None
like image 399
ldevyataykina Avatar asked Feb 20 '26 16:02

ldevyataykina


1 Answers

Would this help?:

for col in types[types=='unicode'].index:
    df[col] = df[col].astype(str)

or:

for col in types[types=='unicode'].index:
     df[col] = df[col].apply(lambda x: x.encode('utf-8').strip())
like image 149
SerialDev Avatar answered Feb 22 '26 07:02

SerialDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!