【问题标题】:How to loop list value of a specific column in pandas?如何循环熊猫中特定列的列表值?
【发布时间】:2017-04-14 18:30:05
【问题描述】:

我有一个熊猫数据框,第一列是列表值。我想循环每个列表的每个 str 值,并将下一列的值包含在一起。

例如:

tm = pd.DataFrame({'author':[['author_a1','author_a2','author_a3'],['author_b1','author_b2'],['author_c1','author_c2']],'journal':['journal01','journal02','journal03'],'date':pd.date_range('2015-02-03',periods=3)})
tm

    author                               date         journal
0   [author_a1, author_a2, author_a3]    2015-02-03   journal01
1   [author_b1, author_b2]               2015-02-04   journal02
2   [author_c1, author_c2]               2015-02-05   journal03

我想要这个:

    author       date          journal
0   author_a1    2015-02-03    journal01
1   author_a2    2015-02-03    journal01
2   author_a3    2015-02-03    journal01
3   author_b1    2015-02-04    journal02
4   author_b2    2015-02-04    journal02
5   author_c1    2015-02-05    journal03
6   author_c2    2015-02-05    journal03

我使用了一种复杂的方法来解决问题。有没有使用pandas的简单高效的方法?

author_use = []
date_use = []
journal_use = []

for i in range(0,len(tm['author'])):    
    for m in range(0,len(tm['author'][i])):
        author_use.append(tm['author'][i][m])
        date_use.append(tm['date'][i])
        journal_use.append(tm['journal'][i])

df_author = pd.DataFrame({'author':author_use,
                         'date':date_use,
                         'journal':journal_use,                        
                         })

df_author

【问题讨论】:

    标签: python loops pandas


    【解决方案1】:

    我认为您可以将numpy.repeat 用于str.len 的长度重复值和chain 的嵌套lists 的平面值:

    from  itertools import chain
    
    lens = tm.author.str.len()
    
    df = pd.DataFrame({
            "date": np.repeat(tm.date.values, lens),
            "journal": np.repeat(tm.journal.values,lens),
            "author": list(chain.from_iterable(tm.author))})
    
    print (df)
    
          author       date    journal
    0  author_a1 2015-02-03  journal01
    1  author_a2 2015-02-03  journal01
    2  author_a3 2015-02-03  journal01
    3  author_b1 2015-02-04  journal02
    4  author_b2 2015-02-04  journal02
    5  author_c1 2015-02-05  journal03
    6  author_c2 2015-02-05  journal03
    

    另一个numpy解决方案:

    df = pd.DataFrame(np.column_stack((tm[['date','journal']].values.\
         repeat(list(map(len,tm.author)),axis=0) ,np.hstack(tm.author))), 
         columns=['date','journal','author'])
    
    print (df)
                      date    journal     author
    0  2015-02-03 00:00:00  journal01  auther_a1
    1  2015-02-03 00:00:00  journal01  auther_a2
    2  2015-02-03 00:00:00  journal01  auther_a3
    3  2015-02-04 00:00:00  journal02  auther_b1
    4  2015-02-04 00:00:00  journal02  auther_b2
    5  2015-02-05 00:00:00  journal03  auther_c1
    6  2015-02-05 00:00:00  journal03  auther_c2
    

    【讨论】:

    • TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe' 怎么了? @jezrael
    • 这个问题是样本还是真实数据?
    • 这个问题与样本有关。
    • 你的python和pandas是什么版本的?
    • Python 2.7.12 |Anaconda 自定义(32 位),pandas 0.19.1
    猜你喜欢
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2016-04-11
    • 2022-11-04
    • 1970-01-01
    • 2017-06-08
    相关资源
    最近更新 更多