Note

Pandas (29) 본문

Pandas

Pandas (29)

알 수 없는 사용자 2022. 8. 21. 21:27
728x90

How to replace missing spaces in a string with the least frequent character?

# Input
my_str = 'dbc deb abed gade'

# Solution
ser = pd.Series(list('dbc deb abed gade'))
freq = ser.value_counts()
print(freq)

# output 
d    4
b    3
e    3
     3
a    2
g    1
c    1
dtype: int64

least_freq = freq.dropna().index[-1]
"".join(ser.replace(' ', least_freq))

 # output
 'dbccdebcabedcgade'

'Pandas' 카테고리의 다른 글

Pandas (31)  (0) 2022.08.23
Pandas (30)  (0) 2022.08.22
Pandas (28)  (0) 2022.08.20
Pandas (27)  (0) 2022.08.19
Pandas (26)  (0) 2022.08.17
Comments