Pankaj Singh Pankaj Singh - 6 days ago 5
Python Question

Python DataFrame from a list

So, I have to create a dataframe. I do not mind my source to be a list of dicts or a dict.

List of Dict:

[{'A': 'First', 'C': 300, 'B': 200},

{'A': 'Second', 'C': 310, 'B': 210},

{'A': 'Third', 'C': 330, 'B': 230},

{'A': 'Fourth', 'C': 340, 'B': 240},

{'A': 'Fifth', 'C': 350, 'B': 250}]


OR

{'First': {'C': 300, 'B': 200},

'Second':{'C': 310, 'B': 210} },

'Third': {'C': 330, 'B': 230},

'Fourth': {'C': 340, 'B': 240},

'Fifth': {'C': 350, 'B': 250} }


I want my dataframe like this

................C........B

First........300.....200

Second...310.....210

Third.......330.....230

Fourth.....340.....240

Fifth........350.....250

Basically, suggesting that one of the columns become the index ...

Answer

Using your first dataset, here is one way:

In [15]: df=pd.DataFrame(l, index=[d['A'] for d in l])

In [16]: del df['A']

In [17]: df
Out[17]: 
          B    C
First   200  300
Second  210  310
Third   230  330
Fourth  240  340
Fifth   250  350

[5 rows x 2 columns]

From your second dataset, here is another way. Note that, since the order of keys in a dictionary is arbitrary, the order of rows in the DataFrame will also be arbitrary.

In [27]: pd.DataFrame(data=l2.values(), index=l2.keys())
Out[27]: 
          B    C
Fifth   250  350
Second  210  310
Fourth  240  340
Third   230  330
First   200  300

[5 rows x 2 columns]