Python str.replace(".", " ") method to rename columns not working

Hi there,

https://app.dataquest.io/m/347/working-with-missing-and-duplicate-data/3/correcting-data-cleaning-errors-that-result-in-missing-values

I am working with the dataframe which is combined —
combined = pd.concat([happiness2015, happiness2016, happiness2017], ignore_index=True,
sort=False)

I ran this line below referring to the above , but no change even the columns with only one dot still appear with a dot.

combined.columns = combined.columns.str.replace(".", " ")

combined.isnull().sum()

before i ran this line and after are the same output as per below photo

I tried looking here but cannot get it to work

https://stackoverflow.com/questions/28503445/assigning-column-names-to-a-pandas-series

Hey @jamesberentsen

As per my understanding by looking at your problem, i think you missed to perform the str.strip() method which basically “Remove leading and trailing characters in Series/Index” . Once you apply this method to your data frame then you should not able to see any (.) in your column names. Please give it a shot and let me know accordingly.

If you required more information on str.strip() method kindly go through the documentation

Best
K!

1 Like

Hi prasadkalyan05

Thanks, I shall try that but it is lengthy doing it for each column, are you sure the problem is leading or trailing spaces?

I thought it removes leading spaces so I do not see how that would include dots as well?

I saw how to do here but it is very lengthy way,since this is needed for each column with problem of leading space even though I am not sure that that is the problem.

df1[State] =df1['State'` `].` `str` `.strip()`

http://www.datasciencemadesimple.com/strip-space-column-pandas-dataframe-leading-trailing-2/

Looked like this is only option ,but it did not work either:
happiness2017.rename(columns={'Economy..GDP.per.Capita.': 'Economy GDP per Capita.'}, inplace=True)

df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)


I tried it on Series but it did not work

Regards
JB

Hi,
@jamesberentsen,
I tried your code by trying it in my notebook and it worked fine.Did u get it solved by the way?

.

I just tried to remove the dots. didn’t do the single space between each words part.

1 Like

Many thanks lakshmial87

That worked.

Regards
JB

1 Like

@jamesberentsen
you are welcome!!! :blush:

1 Like