Remove all quotation marks in values ​​in Pandas

I want to remove all double quotes in all columns and all values ​​in a data framework. Therefore, if I have a value, for example

potatoes are "great"

I want to return

potatoes are great

DataFrame.replace () allows me to do this if I know the whole value that I am changing, but is there a way to remove individual characters?

+4
source share
3 answers

You can do this on each series / column using str.replace :

In [11]: s = pd.Series(['potatoes are "great"', 'they are'])

In [12]: s
Out[12]: 
0    potatoes are "great"
1                they are
dtype: object

In [13]: s.str.replace('"', '')
Out[13]: 
0    potatoes are great
1              they are
dtype: object

I would be wary of this throughout the DataFrame, because it also changed non-strings columns to rows, however you could iterate over each column:

for i, col in enumerate(df.columns):
    df.iloc[:, i] = df.iloc[:, i].str.replace('"', '')

, , applymap:

df.applymap(lambda x: x.replace('"', '')
+11

DataFrame.apply() Series.str.replace():

import numpy as np
import pandas as pd
import random

a = np.array(["".join(random.sample('abcde"', 3)) for i in range(100)]).reshape(10, 10)
df = pd.DataFrame(a)
df.apply(lambda s:s.str.replace('"', ""))

string:

df.ix[:,df.dtypes==object].apply(lambda s:s.str.replace('"', ""))
+4

This will do what you want:

returnlist=[]
for char in string:
    if char != '"':
         returnlist.append(char)
string="".join(returnlist)
+1
source

Source: https://habr.com/ru/post/1524790/


All Articles