python - 如何循环pandas中特定列的列表值?

标签 python loops pandas

我有一个 pandas 数据框,第一列是列表值。我想循环每个列表的每个 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

最佳答案

我认为你可以使用numpy.repeat对于按长度重复的值 str.len以及嵌套列表的平面值:

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

关于python - 如何循环pandas中特定列的列表值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40888274/

相关文章:

python - 使用 python Xlib 重命名窗口

Python 从流中生成 JSON 文档

python - 加速循环以使用另一个数组中最接近的值填充数组

excel - 查找所有匹配项并复制侧列的值 < >""

python - 使用 fill_diagonal() 设置 pandas.DataFrame 对角线上的值

python - Pandas 根据模式计算差异

python - 如何在 Python 2.7 中选择 3 个随机数?

python - "self"里面的普通函数?

java - 使用字符串退出 do while 循环

python - 如何在 python 中创建时间戳的线性空间?