Search code examples
pythonpython-3.xpandasdataframemissing-data

Create data frame with missing values


I want to create a table and fill missing values with my data

The data look like this:

    {'A': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0}
    {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0}

I want to convert the data into pandas data frame with missing values

     A B C D E F G
   1 0 na 0 0 0 0 0
   2 0 0 0 0 0 0 na

I could manually give the missing values (using the below code) and then convert it into data frame. Is there a better way to fill the missing values and convert it into data frame

import pandas as pd

s = (( 0,  'na',  0,  0,  0,  0,  0),
( 0,  0,  0,  0,  0,  0,  'na'))

print (pd.DataFrame(list(s)))

print (pd.DataFrame(list(s), columns=['A', 'B', 'C', 'D', 'E', 'F','G'], index=[1,2]))  

Thanks


Solution

  • If pass list of DataFrame then need only sorting columns names:

    L = [ {'A': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0},
          {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0}]
        
    print (pd.DataFrame(L).sort_index(axis=1))
       A    B  C  D  E  F    G
    0  0  NaN  0  0  0  0  0.0
    1  0  0.0  0  0  0  0  NaN