迭代循环以从字符串中删除特定的不需要的字符

2024-06-28 19:15:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把字符串“APPLES\u10\u4”放在一个数据帧中,并将其变成“APPLES”。我提出的代码如下:

import pandas as pd
data = ['APPLES_10_4']

Name_Parameters = []
df = pd.DataFrame(data, columns = ['fruit'], index = ['count'])

    
def badletters(lastletter):
    badletters = ["1","2","3","4","5","6","7","8","9","_"]
    if lastletter in badletters:
        return True
    else:
        return False   

def stripe(variable):
    tempStrippedVariable = variable
    foundEndVariable = False
    while not foundEndVariable:
        lastletter = tempStrippedVariable [:-1]
        if badletters(lastletter):
            tempStrippedVariable = tempStrippedVariable [:-1]
        else:
            foundEndVariable = True
    strippedVariable = tempStrippedVariable
    return strippedVariable

for variable in df:
strippedVariable = stripe(str(variable))
prefixes = []
if strippedVariable not in prefixes:
    prefixes.append(strippedVariable)
print(df)

我得到的输出是带有['APPLES\u 10\u 4']的原始数据帧,而不是经过修改的数据帧,上面写着['APPLES']。如果这是一个愚蠢的问题,我们将不胜感激


Tags: 数据indfdatareturnifvariablepd
1条回答
网友
1楼 · 发布于 2024-06-28 19:15:59

一些数据帧元素是整数,而不是字符串。您可以在调用stripe()之前将它们转换为字符串

for variable in df:
    strippedVariable = stripe(str(variable))
    if strippedVariable not in prefixes:
        prefixes.append(strippedVariable)
print(prefixes)

或者你可以跳过它们

for variable in df:
    if not isinstance(variable, str):
        continue
    strippedVariable = stripe(variable)
    if strippedVariable not in prefixes:
        prefixes.append(strippedVariable)
print(prefixes)

另一个bug在stripe()中:

lastletter = tempStrippedVariable [:-1]

应该是

lastletter = tempStrippedVariable [-1]

您正在将lastletter设置为除最后一个字母之外的整个字符串

但整个功能可以简单地替换为:

def stripe(variable):
    badletters = ["1","2","3","4","5","6","7","8","9","_"]
    return variable.rstrip(badletters)

最后,for variable in df不遍历数据帧内容,只遍历列名。见How to iterate over rows in a DataFrame in Pandas

for row in df.itertuples():
    variable = row[0]
    strippedVariable = stripe(variable)
    if strippedVariable not in prefixes:
        prefixes.append(strippedVariable)

相关问题 更多 >