【问题标题】:Split List elements into Sublists of words for each element in Pandas Dataframe将列表元素拆分为 Pandas Dataframe 中每个元素的单词子列表
【发布时间】:2020-12-29 07:15:06
【问题描述】:

split list elements into sub-elements in pandas dataframe 这个非常接近 - 但不是将所有元素拆分并放入一个列表中,我需要将它们放入一个子列表中。

df:

col1
['this is list 1', 'this is list 2', 'this is another list 3']
['this is list 1', 'this is list 2', 'this is another list 3']
['this is list 1', 'this is list 2', 'this is another list 3']

我正在尝试使数据框列看起来像这样:

col1
[['this', 'is', 'list', '1'], ['this', 'is', 'list', '2'], ['this', 'is', 'another', lis't', '3']]
[['this', 'is', 'list', '1'], ['this', 'is', 'list', '2'], ['this', 'is', 'another', lis't', '3']]
[['this', 'is', 'list', '1'], ['this', 'is', 'list', '2'], ['this', 'is', 'another', lis't', '3']]

基本上将列表中的元素拆分为子列表,其中每个单词都是子列表中的元素/项目,都在 pandas df 列中

【问题讨论】:

  • 我最初的想法是申请lambda l: [s.split() for s in l]
  • @ifly6 噢噢噢火。做完儿子。谢谢

标签: python pandas


【解决方案1】:

这样做了。谢谢@ifly6

df['col1'] = df. col1.apply(lambda l: [s.split() for s in l])

【讨论】:

    【解决方案2】:

    apply 解决方案是

    s.apply(lambda l: [s.split() for s in l])
    

    我会留给其他人考虑矢量化解决方案。

    【讨论】:

      猜你喜欢
      • 2017-04-25
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多