在python中,如何将字符串小写并将空格更改为下划线?

在python中,如何将字符串小写并将空格更改为下划线?,python,pandas,Python,Pandas,在python中,如何将字符串更改为小写,并将空格更改为下划线 例如,我有一个字符串,地面改善 我希望它是ground\u改进 现在,我知道如何手动使用replace df['type'] = df['type'].replace('Ground Improvement', 'ground_improvement') 但这只是为了改进基础,我想实现一些自动化,因此如果type列中出现任何字符串,脚本将始终更改为我想要的格式 谢谢。在空白处使用lower和replace df['type'] =

在python中,如何将字符串更改为小写,并将空格更改为下划线

例如,我有一个字符串,
地面改善

我希望它是
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

请发布带有预期输出的示例输入数据,以便更好地理解。