替换pandas数据框中的一列中的列表内的项目?

时间:2019-03-07 19:41:16

标签: python regex 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++']]})

如果我想将R[space]的{​​{1}}和R,替换为R的{​​{1}}和C[space],应该怎么做?

1 个答案:

答案 0 :(得分:2)

使用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++]