Removing space in dataframe python

会有一股神秘感。 提交于 2019-11-30 09:19:23

Try:

formatted.columns = [x.strip().replace(' ', '_') for x in formatted.columns]

As I understand your question, the following should work (test it out with inplace=False to see how it looks first if you want to be careful):

sortedtotal.rename(columns=lambda x: x.replace(" ", "_"), inplace=True)

And if you have white space surrounding the column names, like: "This example "

sortedtotal.rename(columns=lambda x: x.strip().replace(" ", "_"), inplace=True)

which strips leading/trailing whitespace, then converts internal spaces to "_".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!