I would like to organize the dictionary list in Python in Excel 2D table.

Asked 2 years ago, Updated 2 years ago, 97 views

I'm a beginner who just entered Python. I want to make a program that filters through Pandas by making a dictionary list into a two-dimensional table, how should I organize this? The list I have is in this format.

list = [
    {
        "Name" : "Jerry",
        "No." : 33425,
        "Type" : "Programmer"
    },
    {
        "Name" : "Petter",
        "No." : 55324,
        "Sex" : "Male"
    },
    ...

Like this, the key values in the Dictionary are different for each item, so I am having a hard time organizing them. This is actually the biggest problem. Is there a way to organize key:value pairs separately by item?

python2.7

2022-09-20 22:00

1 Answers

If you just hand over the dictionary list as a factor for the pd.DataFrame constructor, it makes it a data frame on its own.

>>> data = [
    {
        "Name" : "Jerry",
        "No." : 33425,
        "Type" : "Programmer"
    },
    {
        "Name" : "Petter",
        "No." : 55324,
        "Sex" : "Male"
    }]
>>> df = pd.DataFrame(data)
>>> df
     Name    No.   Sex        Type
0   Jerry  33425   NaN  Programmer
1  Petter  55324  Male         NaN

Here, those without a key go into NaN as shown in the example above, and I think we need to take care of it for each column. In the case of Sex, replace NaN with Unknown or Type with Unknown... You have to do it according to the purpose. You can use the fillna function to replace it.


2022-09-20 22:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.