Pankaj Singh
Pankaj Singh

Reputation: 586

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 ...

Upvotes: 0

Views: 73

Answers (3)

Vidhya G
Vidhya G

Reputation: 2320

List of Dict:

l = [{'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}]


df = pd.DataFrame.from_records(l).set_index('A')

Dictionary:

d = {'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} }

df = pd.DataFrame.from_dict(d, orient='index')

Upvotes: 2

akuiper
akuiper

Reputation: 214957

Also you can use pd.DataFrame.from_records() where you can set a specific column to be index:

pd.DataFrame.from_records([{'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}], index = ['A'])

enter image description here

Upvotes: 3

Robᵩ
Robᵩ

Reputation: 168626

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]

Upvotes: 2

Related Questions