简体   繁体   English

为什么替换子字符串在 Pandas 数据框中不起作用?

[英]Why replace substring does not work in Pandas dataframe?

I try to replace everywhere the symbols " - in the start line and end line:我尝试在起始行和结束行中替换所有符号" -

dtnew.applymap(lambda x: x.replace('^-', ''))
dtnew.applymap(lambda x: x.replace('^"', ''))

But the output dataframe has these symbols但是输出数据框有这些符号

well, if performance is NOT an issue you can iterate over columns and rows and use a simple replace (see below).好吧,如果性能不是问题,您可以遍历列和行并使用简单的替换(见下文)。 Again, I would only use this if the dataframe is not enormous and you have no concern for performance.同样,如果数据框不是很大并且您不关心性能,我只会使用它。

for column in df.columns:
    for i in df.index:    
        df[column][i] = df[column][i].replace('-','').replace('"','')

Assuming this example and that you only want to replace the leading character(s):假设此示例并且您只想替换前导字符:

df = pd.DataFrame([['- abc', 'def -'], ['" ghi-', '--jkl']])

        0      1
0   - abc  def -
1  " ghi-  --jkl

Use str.lstrip .使用str.lstrip

df2 = df.apply(lambda c: c.str.lstrip('- "'))

output:输出:

      0      1
0   abc  def -
1  ghi-    jkl

# as list: [['abc', 'def -'], ['ghi-', 'jkl']]

For only the first character, use str.replace :仅对于第一个字符,使用str.replace

df2 = df.apply(lambda c: c.str.replace('^[- "]', '', regex=True))

output:输出:

       0      1
0    abc  def -
1   ghi-   -jkl

# as list: [[' abc', 'def -'], [' ghi-', '-jkl']]

generalization:概括:

  • to strip both start and end, use str.strip剥离开始和结束,使用str.strip

  • to remove all characters (anywhere): df.apply(lambda c: c.str.replace('[- "]', '', regex=True))删除所有字符(任何地方): df.apply(lambda c: c.str.replace('[- "]', '', regex=True))

  • to remove first or last matching character: df.apply(lambda c: c.str.replace('(^[- "]|[- "]$)', '', regex=True))删除第一个或最后一个匹配字符: df.apply(lambda c: c.str.replace('(^[- "]|[- "]$)', '', regex=True))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM