0

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

1
  • I am using dataFrame. Problem is I am not able to figure out how to make contents of dict index 'A' as index of the dataframe Commented Nov 30, 2016 at 18:43

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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]

1 Comment

x = pd.DataFrame(data,index=[x['A'] for x in data]) ... yeah, that is what I was wondering too... thanks Rob
2

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')

Comments

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.