Pandas - 修改每个单元格中的字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38572815/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:40:02  来源:igfitidea点击:

Pandas - Modify string values in each cell

pythonpandas

提问by code base 5000

I have a panda dataframe and I need to modify all values in a given column. Each column will contains a string value of the same length. The user provides the index they want to be replaced for each value: ex: [1:3]and the replacement value "AAA".

我有一个Pandas数据框,我需要修改给定列中的所有值。每列将包含一个相同长度的字符串值。用户为每个值提供他们想要替换的索引:例如:[1:3]和替换值"AAA"

This would replace the string from values 1 to 3 with the value AAA.

这会将值 1 到 3 的字符串替换为值AAA

How can I use the applymap, map or apply function to get this done?

如何使用 applymap、map 或 apply 函数来完成这项工作?

Thanks

谢谢

Here is the final solution I went off of using the answer marked below:

这是我使用下面标记的答案的最终解决方案:

import pandas as pd

df = pd.DataFrame({'A':['ffgghh','ffrtss','ffrtds'],
                     #'B':['ffrtss','ssgghh','d'],
                     'C':['qqttss',' 44','f']})
print df
old = ['g', 'r', 'z']
new = ['y', 'b', 'c']
vals = dict(zip(old, new))

pos = 2
for old, new in vals.items():
    df.ix[df['A'].str[pos] == old, 'A'] = df['A'].str.slice_replace(pos,pos + len(new),new)
print df

回答by root

Use str.slice_replace:

使用str.slice_replace

df['B'] = df['B'].str.slice_replace(1, 3, 'AAA')

Sample Input:

样本输入:

   A         B
0  w   abcdefg
1  x   bbbbbbb
2  y   ccccccc
3  z  zzzzzzzz

Sample Output:

示例输出:

   A          B
0  w   aAAAdefg
1  x   bAAAbbbb
2  y   cAAAcccc
3  z  zAAAzzzzz

回答by MaxU

IMO the most straightforward solution:

IMO 最直接的解决方案:

In [7]: df
Out[7]:
        col
0   abcdefg
1   bbbbbbb
2   ccccccc
3  zzzzzzzz

In [9]: df.col = df.col.str[:1] + 'AAA' + df.col.str[4:]

In [10]: df
Out[10]:
        col
0   aAAAefg
1   bAAAbbb
2   cAAAccc
3  zAAAzzzz