Pandas Data Series: Replace missing white spaces in a given string with the least frequent character
Pandas: Data Series Exercise-33 with Solution
Write a Pandas program to replace missing white spaces in a given string with the least frequent character.
Sample Solution :
Python Code :
import pandas as pd str1 = 'abc def abcdef icd' print("Original series:") print(str1) ser = pd.Series(list(str1)) element_freq = ser.value_counts() print(element_freq) current_freq = element_freq.dropna().index[-1] result = "".join(ser.replace(' ', current_freq)) print(result)
Original series: abc def abcdef icd c 3 3 d 3 f 2 e 2 a 2 b 2 i 1 dtype: int64 abcidefiabcdefiicd
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to find the positions of the values neighboured by smaller values on both sides in a given series.
Next: Write a Pandas program to compute the autocorrelations of a given numeric series.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Returns a list with n elements removed from the beginning
def tips_take(itr, n = 1): return itr[:n] print(tips_take([1, 2, 3], 5)) print(tips_take([1, 2, 3], 0))
[1, 2, 3] 
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework