删除Pandas中值内的所有引号

Sat*_*eri 6 python dataframe pandas

我想删除所有列中的所有双引号和数据帧中的所有值.所以,如果我有一个像这样的价值

potatoes are "great"
Run Code Online (Sandbox Code Playgroud)

我想回来

potatoes are great
Run Code Online (Sandbox Code Playgroud)

DataFrame.replace()允许我这样做,如果我知道我正在改变的整个值,但有没有办法删除单个字符?

And*_*den 15

您可以使用str.replace在每个Series /列上执行此操作:

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
Run Code Online (Sandbox Code Playgroud)

我会担心在整个DataFrame中执行此操作,因为它还会将非字符串列更改为字符串,但是您可以迭代每列:

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

如果您确定每个项目都是字符串,则可以使用applymap:

df.applymap(lambda x: x.replace('"', ''))
Run Code Online (Sandbox Code Playgroud)


HYR*_*YRY 5

使用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('"', ""))
Run Code Online (Sandbox Code Playgroud)

如果只是string列:

df.ix[:,df.dtypes==object].apply(lambda s:s.str.replace('"', ""))
Run Code Online (Sandbox Code Playgroud)