我有一个名为 df
的 df,就像这样。 tag_position 是字符串或列表。但我希望它们都是字符串。我怎样才能做到这一点?我还想删除末尾的空白区域。
输入
id tag_positions
1 center
2 right
3 ['left']
4 ['center ']
5 [' left']
6 ['right']
7 left
预期输出
id tag_positions
1 center
2 right
3 left
4 center
5 left
6 right
7 left
最佳答案
你可以爆炸
然后剥离
:
df.tag_positions = df.tag_positions.explode().str.strip()
得到
id tag_positions
0 1 center
1 2 right
2 3 left
3 4 center
4 5 left
5 6 right
6 7 left
关于python - 将列表列转换为 Pandas 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67285804/