katus
katus

Reputation: 669

Replace items inside a list in a column in pandas dataframe?

Suppose I have a pandas df:

import pandas as pd
ex = pd.DataFrame({'Title': ['Data Scientist', 'Data Analyst', 'Data Engineer'], 
                   'Skills': [['Python', 'R ', 'C ', 'C++'], ['Python', 'R,', 'C ', 'C++'], ['Python', 'R ', 'C,', 'C++']]})

If I want to replace R[space] and R, with R and C[space] and C, with C, how should I do it?

Upvotes: 0

Views: 869

Answers (1)

BENY
BENY

Reputation: 323326

Using strip

ex.Skills=[[y.strip() for y in x] for x in ex.Skills]
ex
Out[755]: 
            Title               Skills
0  Data Scientist  [Python, R, C, C++]
1    Data Analyst  [Python, R, C, C++]
2   Data Engineer  [Python, R, C, C++]

Upvotes: 2

Related Questions