我的 Pandas 数据框中有一个列表作为单元格的值。我需要在列表的第一个元素的值前添加一个空格,并在最后一个值中添加“和”。
如果它被写成一个不在 Pandas 或 dataframe 中的常规列表,它看起来像这样:
# add a space before the first element
list[0] = ' ' + list[0]
# add 'and' for the last element
list[-1] = 'and' + list[-1]
但现在该列表在 df 中。像这样。
col_A
0 [abc, abc, abc, abc]
1 [abc, abc, abc, abc]
3 [abc, abc, abc, abc]
所以我想我需要使用 lambda,但我不确定.. 这是我尝试过的,但没有奏效:
df['col_a'].apply(lambda x: ' ' + x[0])
返回错误是:
AttributeError: 'list' object has no attribute 'apply'
提前致谢。