1

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

1 Answer 1

1

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
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I can get the column names in order

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.