在python中,如何将字符串小写并将空格更改为下划线?
在python中,如何将字符串更改为小写,并将空格更改为下划线 例如,我有一个字符串,在python中,如何将字符串小写并将空格更改为下划线?,python,pandas,Python,Pandas,在python中,如何将字符串更改为小写,并将空格更改为下划线 例如,我有一个字符串,地面改善 我希望它是ground\u改进 现在,我知道如何手动使用replace df['type'] = df['type'].replace('Ground Improvement', 'ground_improvement') 但这只是为了改进基础,我想实现一些自动化,因此如果type列中出现任何字符串,脚本将始终更改为我想要的格式 谢谢。在空白处使用lower和replace df['type'] =
地面改善
我希望它是ground\u改进
现在,我知道如何手动使用replace
df['type'] = df['type'].replace('Ground Improvement', 'ground_improvement')
但这只是为了改进基础,我想实现一些自动化,因此如果type
列中出现任何字符串,脚本将始终更改为我想要的格式
谢谢。在空白处使用
lower
和replace
df['type'] = df['type'].str.replace(' ','_').str.lower()
输入:
df = pd.DataFrame({'type':['Ground Improvement']})
df
看起来像这样
type
0 Ground Improvement
输出
type
0 ground_improvement
只有两行:-
s = "Ground Improvement"
s = str.lower() # Converts the entire string to lower case
s = str.replace(' ', '_') # Replaces the spaces with _
在那里,您有
ground\u改进
存储在s
中 请发布带有预期输出的示例输入数据,以便更好地理解。