Python 删除 Pandas 中值中的所有引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21491291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove all quotes within values in Pandas
提问by Satvik Beri
I want to remove all double quotes within all columns and all values in a dataframe. So if I have a value such as
我想删除所有列中的所有双引号和数据框中的所有值。所以如果我有一个价值,比如
potatoes are "great"
I want to return
我想回来
potatoes are great
DataFrame.replace() lets me do this if I know the entire value I'm changing, but is there a way to remove individual characters?
如果我知道要更改的整个值,DataFrame.replace() 可以让我这样做,但是有没有办法删除单个字符?
采纳答案by Andy Hayden
You can do this on each Series/column using str.replace:
您可以使用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 doing this across the entire DataFrame, because it will also change columns of non-strings to strings, however you could iterate over each column:
我会警惕在整个 DataFrame 中执行此操作,因为它还会将非字符串列更改为字符串,但是您可以遍历每一列:
for i, col in enumerate(df.columns):
df.iloc[:, i] = df.iloc[:, i].str.replace('"', '')
If you were sure every item was a string, you could use applymap:
如果您确定每个项目都是一个字符串,则可以使用applymap:
df.applymap(lambda x: x.replace('"', ''))
回答by Synthetica
This will do what you want:
这将执行您想要的操作:
returnlist=[]
for char in string:
if char != '"':
returnlist.append(char)
string="".join(returnlist)
回答by HYRY
use DataFrame.apply()and Series.str.replace():
使用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('"', ""))
If just stringcolumns:
如果只是string列:
df.ix[:,df.dtypes==object].apply(lambda s:s.str.replace('"', ""))

