Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 .replace不从数据中删除字符_Python 2.7_Pandas - Fatal编程技术网

Python 2.7 .replace不从数据中删除字符

Python 2.7 .replace不从数据中删除字符,python-2.7,pandas,Python 2.7,Pandas,我有一个熊猫数据框,在某些地方带有字符“(Python 2.7)。 我想从数据中删除所有的“。我使用以下方法: data_frame.replace(to_replace'"', value = '') 但是,数据帧保持不变,操作不会发生。 如果您能提供有关问题所在的任何建议,我们将不胜感激。请将就地标志设置为真,或者将输出重新分配回数据帧: data_frame.replace(to_replace'"', value = '', inplace=True) 或 replace函数返回带有

我有一个熊猫数据框,在某些地方带有字符
(Python 2.7)。 我想从数据中删除所有的
。我使用以下方法:

data_frame.replace(to_replace'"', value = '')
但是,数据帧保持不变,操作不会发生。
如果您能提供有关问题所在的任何建议,我们将不胜感激。

请将
就地
标志设置为
,或者将输出重新分配回
数据帧

data_frame.replace(to_replace'"', value = '', inplace=True)


replace
函数返回带有替换数据的新数据帧。尝试:

data_frame = data_frame.replace(to_replace='"', value='')

您需要使用
系列的
str.replace
方法

因此:

foo
作为列的名称

> df
    foo
0  "bar"

> df.foo.str.replace('"', '')
0    bar
Name: foo, dtype: object
如果你有很多专栏,但是@jezrael答案更好,我想:

for s in df:
    if df[s].dtype == "object": # to avoid converting non-string column into string
        df.loc[:,s] = df.loc[:,s].str.replace('"', '')
您可以尝试使用
regex=True

import pandas as pd

df = pd.DataFrame({'ItemID': {0: 8988, 1: 8988, 2: 6547, 3: 6547}, 
                   'Description': {0: 'Tall Chair', 1: 'Tall Chair', 2: 'Big" Pillow', 3: 'Big Pillow'}, 
                   'Feedback': {0: 'I hated it""', 1: 'Best chair" ever', 2: 'Soft and amazing', 3: 'Horrific color'}})
print df
   Description          Feedback  ItemID
0   Tall Chair      I hated it""    8988
1   Tall Chair  Best chair" ever    8988
2  Big" Pillow  Soft and amazing    6547
3   Big Pillow    Horrific color    6547

print df.replace({'"': ''}, regex=True)
  Description          Feedback  ItemID
0  Tall Chair        I hated it    8988
1  Tall Chair   Best chair ever    8988
2  Big Pillow  Soft and amazing    6547
3  Big Pillow    Horrific color    6547

啊,很好,在数据帧上比我的答案更有效。谢谢。我认为你的答案非常适合在一列中替换。谢谢!replace({'':''},regex=True)工作得很好!
for s in df:
    if df[s].dtype == "object": # to avoid converting non-string column into string
        df.loc[:,s] = df.loc[:,s].str.replace('"', '')
import pandas as pd

df = pd.DataFrame({'ItemID': {0: 8988, 1: 8988, 2: 6547, 3: 6547}, 
                   'Description': {0: 'Tall Chair', 1: 'Tall Chair', 2: 'Big" Pillow', 3: 'Big Pillow'}, 
                   'Feedback': {0: 'I hated it""', 1: 'Best chair" ever', 2: 'Soft and amazing', 3: 'Horrific color'}})
print df
   Description          Feedback  ItemID
0   Tall Chair      I hated it""    8988
1   Tall Chair  Best chair" ever    8988
2  Big" Pillow  Soft and amazing    6547
3   Big Pillow    Horrific color    6547

print df.replace({'"': ''}, regex=True)
  Description          Feedback  ItemID
0  Tall Chair        I hated it    8988
1  Tall Chair   Best chair ever    8988
2  Big Pillow  Soft and amazing    6547
3  Big Pillow    Horrific color    6547