Skip to content
Advertisement

How to zip together lists of unequal length into a dictionary?

I have three lists.

import pandas as pd

author = ['mccoy.robert']
coauthors = [
    'hola.lubica', 'kundu.subiman', 'ntantu.ibula', 
    'fletcher.peter', 'jain.tanvi', 'jindal.varun', 'bankston.paul',
    'di-maio.giuseppe', 'dickman.raymond-f-jun', 'holy.dusan',
    'slover.rebecca', 'curtis.douglas-w', 'duvall.paul-f', 'fogelgren.j-r',
    'hammer.s-t', 'haworth.r-c', 'lutzer.david-j', 'okuyama.akihiro', 
    'pelant.jan', 'porter.jack-ray', 'raha.asit-baran', 'rubin.leonard-roy',
    'santoro.grazia', 'smith.jerome-c', 'todd.aaron-r'
]
frequency = [
    '10', '8', '6', '5', '3', '3', '2', '2', '2', '2', '2', 
    '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'
]

I’d like to make these lists into a dataframe in which each row is the author paired with each coauthor and the frequency of collaborations. Basically if I could zip these three lists together, with the list ‘author’ replicated over and over in each row, this would be great.

This seems like a fairly simple pandas task… but I’d love some help on it!

Advertisement

Answer

You can do this with nested for loop, looping over the authors and the zipped coauthors and frequencies. Like so:

authors = ['mccoy.robert']
coauthors = [
    'hola.lubica', 'kundu.subiman', 'ntantu.ibula', 
    'fletcher.peter', 'jain.tanvi', 'jindal.varun', 'bankston.paul',
    'di-maio.giuseppe', 'dickman.raymond-f-jun', 'holy.dusan',
    'slover.rebecca', 'curtis.douglas-w', 'duvall.paul-f', 'fogelgren.j-r',
    'hammer.s-t', 'haworth.r-c', 'lutzer.david-j', 'okuyama.akihiro', 
    'pelant.jan', 'porter.jack-ray', 'raha.asit-baran', 'rubin.leonard-roy',
    'santoro.grazia', 'smith.jerome-c', 'todd.aaron-r'
]
frequencies = [
    '10', '8', '6', '5', '3', '3', '2', '2', '2', '2', '2', 
    '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'
]

combined = []

for author in authors:
    for coauthor, frequency in zip(coauthors, frequencies):
        combined.append((author, coauthor, frequency))

With Output:

[
    ('mccoy.robert', 'hola.lubica', '10'), ('mccoy.robert', 'kundu.subiman', '8'), ('mccoy.robert', 'ntantu.ibula', '6'),
    ('mccoy.robert', 'fletcher.peter', '5'), ('mccoy.robert', 'jain.tanvi', '3'), ('mccoy.robert', 'jindal.varun', '3'),
    ('mccoy.robert', 'bankston.paul', '2'), ('mccoy.robert', 'di-maio.giuseppe', '2'), ('mccoy.robert', 'dickman.raymond-f-jun', '2'),
    ('mccoy.robert', 'holy.dusan', '2'), ('mccoy.robert', 'slover.rebecca', '2'), ('mccoy.robert', 'curtis.douglas-w', '1'),
    ('mccoy.robert', 'duvall.paul-f', '1'), ('mccoy.robert', 'fogelgren.j-r', '1'), ('mccoy.robert', 'hammer.s-t', '1'),
    ('mccoy.robert', 'haworth.r-c', '1'), ('mccoy.robert', 'lutzer.david-j', '1'), ('mccoy.robert', 'okuyama.akihiro', '1'),
    ('mccoy.robert', 'pelant.jan', '1'), ('mccoy.robert', 'porter.jack-ray', '1'), ('mccoy.robert', 'raha.asit-baran', '1'),
    ('mccoy.robert', 'rubin.leonard-roy', '1'), ('mccoy.robert', 'santoro.grazia', '1'), ('mccoy.robert', 'smith.jerome-c', '1'),
    ('mccoy.robert', 'todd.aaron-r', '1')
]
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement