Search code examples
pythondataframegoogle-colaboratory

How do you make lists the same length by adding in N/A into all of them?


I have a dictionary:

gene_table_comparison = {
    "index":[1,2,3,4,5],
    "GeneID_1":["a","b","c","d","e"],
    "Start_1":[100,200,300,400,500]
    "Function_1":["Bruh","","Dude","","Seriously"],
    "GeneID_2":[1,2,3],
    "Start_2":["x",y","z"],
    "Function_2":["Geez","","Deez"]
}

and I want to convert it into a data frame by using pd.DataFrame(gene_table_comparison).

It requires to make each of the lists the same length though, and I would like N/A s to be at the end of each list, but how do I do that? And what if they are each different/random lengths?


Solution

  • Here's a one-liner that works:

    gene_table_comparison = {
        "index":[1,2,3,4,5],
        "GeneID_1":["a","b","c","d","e"],
        "Start_1":[100,200,300,400,500],
        "Function_1":["Bruh","","Dude","","Seriously"],
        "GeneID_2":[1,2,3],
        "Start_2":["x","y","z"],
        "Function_2":["Geez","","Deez"]
    }
    
    dict_df = pd.DataFrame({ key:pd.Series(value) for key, value in gene_table_comparison.items() })