更改列的值样式python

更改列的值样式python,python,pandas,dst,Python,Pandas,Dst,我的数据集如下所示: 我的问题是在anotation列中,我想将上面显示的列表样式更改为以下内容: [flight-sth] 我的意思是,Anotion列值有多种样式,我只想将该样式更改为我的样式: 例如: 完成此转换后,将其完全替换为旧的anotation列: 涂鸦样本: anotation ['flight_search.destination1'] ['flight_search.origin'] ['flight_search.destination1'] ['flight

我的数据集如下所示:

我的问题是在anotation列中,我想将上面显示的列表样式更改为以下内容:

[flight-sth] 我的意思是,Anotion列值有多种样式,我只想将该样式更改为我的样式: 例如:

完成此转换后,将其完全替换为旧的anotation列:

涂鸦样本:

anotation
['flight_search.destination1']  
['flight_search.origin']  
['flight_search.destination1']  
['flight_search.type']  
['flight_search.type']  
['flight_search.airline']  
['flight_search.stops']  
['flight_search.stops']  
['flight_search.price_range']  
['flight_search.price_range']  
['flight1_detail.from.time']  
['flight_search.date.depart_origin']  
输出

数据帧

我相信如果没有拼写错误的话,这应该可以做到

输出

数据帧


我相信这应该可以做到,如果没有输入错误,Python字符串替换方法可能是一种选择。但是我知道你希望第一个下划线是a,第二个是空格。我认为,如果深入研究python中的正则表达式,这个问题就可以解决。为了简单起见,到目前为止,我已经做了以下几点:

mystring = 'flight_search.price_range'
mystring = mystring.replace("_", "-")
mystring = mystring.replace(".", " ")

编辑代码:

mystring = 'flight_search.price_range'
mystring = mystring.replace("_", "-",1)
mystring = mystring.replace(".", " ")
mystring = mystring.replace("_", " ")
print(mystring)
编辑代码的结果:
航班搜索价格范围可以选择Python字符串替换方法。但是我知道你希望第一个下划线是a,第二个是空格。我认为,如果深入研究python中的正则表达式,这个问题就可以解决。为了简单起见,到目前为止,我已经做了以下几点:

mystring = 'flight_search.price_range'
mystring = mystring.replace("_", "-")
mystring = mystring.replace(".", " ")

编辑代码:

mystring = 'flight_search.price_range'
mystring = mystring.replace("_", "-",1)
mystring = mystring.replace(".", " ")
mystring = mystring.replace("_", " ")
print(mystring)
编辑代码的结果:
航班搜索价格范围

您需要考虑的是需要对注释列中的字符串进行哪些更改。使用df.replace函数,您可以对所有列应用简单的更改

但是,如果您需要更多的控制,则需要使用df.apply函数。使用此函数,您可以使用自定义函数精确指定要对列中的每个字符串执行的操作

例如,您可以采用这种方法开始,您可以更改自定义函数以获得所需的结果:

import pandas as pd

annotation = ['flight_search.destination1',  
'flight_search.origin',
'flight_search.destination1',
'flight_search.type' ,
'flight_search.type'  ,
'flight_search.airline',  
'flight_search.stops'  ,
'flight_search.stops'  ,
'flight_search.price_range' ,
'flight_search.price_range' ,
'flight1_detail.from.time' ,
'flight_search.date.depart_origin']

df = pd.DataFrame({"annotation":annotation})

def custom_func(string):
    # replace the initial word
    string = string.replace("flight_", "flight-")
    string = string.replace("flight1_", "flight1-") # is this a typo?
    
    # replace the other punctuataion marks with a space
    for punctuation in ['_', '.']:
        string = string.replace(punctuation, " ")
    
    # retun the formatted string
    return string

# apply the custom function to the annotation column
df["annotation"] = df["annotation"].apply(custom_func)

您需要考虑的是需要对注释列中的字符串进行哪些更改。使用df.replace函数,您可以对所有列应用简单的更改

但是,如果您需要更多的控制,则需要使用df.apply函数。使用此函数,您可以使用自定义函数精确指定要对列中的每个字符串执行的操作

例如,您可以采用这种方法开始,您可以更改自定义函数以获得所需的结果:

import pandas as pd

annotation = ['flight_search.destination1',  
'flight_search.origin',
'flight_search.destination1',
'flight_search.type' ,
'flight_search.type'  ,
'flight_search.airline',  
'flight_search.stops'  ,
'flight_search.stops'  ,
'flight_search.price_range' ,
'flight_search.price_range' ,
'flight1_detail.from.time' ,
'flight_search.date.depart_origin']

df = pd.DataFrame({"annotation":annotation})

def custom_func(string):
    # replace the initial word
    string = string.replace("flight_", "flight-")
    string = string.replace("flight1_", "flight1-") # is this a typo?
    
    # replace the other punctuataion marks with a space
    for punctuation in ['_', '.']:
        string = string.replace(punctuation, " ")
    
    # retun the formatted string
    return string

# apply the custom function to the annotation column
df["annotation"] = df["annotation"].apply(custom_func)

谢谢你的回答,但是,我想先转换成-。您可以将所有样本转换为-查看anotation中的最后一个样本column@MaryamiNajafi哥特楚!我的不好,但我相信您现在能够理解/理解replace函数的工作原理了^ ^谢谢您的回答,但是,我想先把它转换为-。您可以将所有样本转换为-查看anotation中的最后一个样本column@MaryamiNajafi哥特楚!我的不好,但我相信您现在能够理解/理解replace函数是如何工作的^ ^谢谢您的回答,但是,我想首先将其转换为-。你把所有的s都转换成-看到最后一个样本在anotation栏@jimmy smeijstersI认为我更新了我的答案,但显然没有。。。在这里,您可以看到如何使replace函数只替换下划线一次,然后将其余的下划线改为空格:请参见编辑部分谢谢您的回答,但我只想先将其转换为-。你把所有的s都转换成-看到最后一个样本在anotation栏@jimmy smeijstersI认为我更新了我的答案,但显然没有。。。在这里,您可以看到如何使replace函数只替换下划线一次,然后将其余下划线更改为空格:请参阅编辑的partPerfect,认为它会起作用:非常感谢@andrebut,我如何解决此问题:[“航班已预订。航班已预订”]航班uu使此示例获得-而不是预定航班中的空间u:我发现这部分的正确答案是:```string=string.replaceflight uu,航班-,1``很高兴您找到了解决方案!您确实可以使用str.replace函数指定替换的数量!太好了,我想这会有用:非常感谢@andrebut,我该如何解决这个问题:['flight\u booked.flight\u booked']flight\u让这个示例得到-而不是flight中的空间\u booked:我发现这部分的正确答案是:```string=string.replaceflight\u,flight-,1``很高兴你找到了解决方案!您确实可以使用str.replace函数指定替换的数量!
mystring = 'flight_search.price_range'
mystring = mystring.replace("_", "-",1)
mystring = mystring.replace(".", " ")
mystring = mystring.replace("_", " ")
print(mystring)
import pandas as pd

annotation = ['flight_search.destination1',  
'flight_search.origin',
'flight_search.destination1',
'flight_search.type' ,
'flight_search.type'  ,
'flight_search.airline',  
'flight_search.stops'  ,
'flight_search.stops'  ,
'flight_search.price_range' ,
'flight_search.price_range' ,
'flight1_detail.from.time' ,
'flight_search.date.depart_origin']

df = pd.DataFrame({"annotation":annotation})

def custom_func(string):
    # replace the initial word
    string = string.replace("flight_", "flight-")
    string = string.replace("flight1_", "flight1-") # is this a typo?
    
    # replace the other punctuataion marks with a space
    for punctuation in ['_', '.']:
        string = string.replace(punctuation, " ")
    
    # retun the formatted string
    return string

# apply the custom function to the annotation column
df["annotation"] = df["annotation"].apply(custom_func)