D
D
Denis Lvov2020-06-04 13:36:12
Python
Denis Lvov, 2020-06-04 13:36:12

How to remove single quote from pandas?

Good afternoon!
There is the following code:

import dask.dataframe
import pandas as pd

file_csv = r'D:\phone2.csv'

df = dask.dataframe.read_csv(file_csv, encoding='windows-1251', sep='\\t',  engine='python')
#df.columns = df.columns.str.strip('"')           #1 Попытка убрать кавычки 
df.columns = df.columns.str.replace('"','')       #2 Попытка
df.

At the exit

phone | status
-------------------------------
"70000000000 | ok"
"71111111111 | ok"

those. one quote in each column. Can anyone suggest how to remove them.
ps quotechar parameter in read_csv not working

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-04
@LazyTalent

df = pd.DataFrame([{'name': '"name1"', 'status': '"ok"'}, {'name': '"name2"', 'status': '"ok"'}])
>>> df
      name status
0  "name1"   "ok"
1  "name2"   "ok"

one.
>>> for col in df.columns.values:
...     df[col] = df[col].str.strip('"')
... 
>>> df
    name status
0  name1     ok
1  name2     ok

2.
>>> df[df.columns] = df.apply(lambda x: x.str.strip('"'))
>>> df
    name status
0  name1     ok
1  name2     ok

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question