【问题标题】:Finding matched elements between rows (list of words) of a Pandas series & a given list在 Pandas 系列的行(单词列表)和给定列表之间查找匹配的元素
【发布时间】:2019-08-10 04:43:35
【问题描述】:

我有一个熊猫系列,其中每一行都是单词列表。 Example Series

我有一个单词列表,比如 my_list = ['ball', 'apple']。我想用出现在该行和 my_list 中的单词替换该系列的所有行。最快的方法是什么?向量化可以用于此类任务吗?

我希望结果如下:Desired Output

【问题讨论】:

    标签: python pandas numpy vectorization


    【解决方案1】:

    试试这个:

    df = pd.DataFrame({'col1':[['apple','ball','cat'],['cat','donkey'],['elephant','apple','rhino']]}, index=[0,1,2])
    
    my_list = ['ball', 'apple']
    
    pd.Series([[r for r in i if r in my_list] for i in df['col1']])
    

    输出:

    0    [apple, ball]
    1               []
    2          [apple]
    dtype: object
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      相关资源
      最近更新 更多