简体   繁体   English

替换字符串中的最后一个字符,如果它是 dataframe 中的特定字符 Python Pandas

[英]Replace the last character in a string if it's a particular character in a dataframe Python Pandas

I have a dataframe like below.我有一个如下所示的 dataframe。 If there's a character in the last position is a dot, I want to replace it as a character "K", not sure how to add the condition in the replace function如果最后position中有一个字符是点,我想将其替换为字符“K”,不知道如何在替换function中添加条件

df = pd.DataFrame({ 'Mix':['572.7.','44.44','99']})

df['Mix'].str.replace('.','K',regex=False)

Sample data样本数据

在此处输入图像描述

expected result预期结果

在此处输入图像描述

Using regex to match the last character:使用正则表达式匹配最后一个字符:

df['Mix'].str.replace('\.$','K',regex=True)

Without regex you could use .str.endswith('.') or .str[-1] == '.'如果没有正则表达式,您可以使用.str.endswith('.').str[-1] == '.' to filter rows which need to replace last char过滤需要替换最后一个字符的行

mask = df['Mix'].str.endswith('.')
#mask = (df['Mix'].str[-1] == '.')

df['Mix'][mask] = df['Mix'][mask]....

But problem is replace - it would replace all dots in text.但问题是replace - 它会替换文本中的所有点。 It would need to use different method - get text without last char .str[:-1] and add new char + "K"它需要使用不同的方法 - 获取没有最后一个字符的文本.str[:-1]并添加新的字符+ "K"

df['Mix'][mask] = df['Mix'][mask].str[:-1] + 'K'

import pandas as pd

df = pd.DataFrame({ 'Mix':['572.7.','44.44','99']})

mask = df['Mix'].str.endswith('.')
#mask = (df['Mix'].str[-1] == '.')

df['Mix'][mask] = df['Mix'][mask].str[:-1] + 'K'

print(df)

EDIT:编辑:

Similar method with apply() (also without regex )apply()类似的方法(也没有regex

def modify(text):
    if text.endswith('.'):
        text = text[:-1] + 'K'
    return text

df['Mix'] = df['Mix'].apply(modify)

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

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