Issue
I want to ask a question about panda's in python - specifically about it's DataFrame()
function.
I have the following data that I want to covert to a data frame:
pop = {'Nevada': {2001: 2.4, 2002: 2.9}, 'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
frame3 = pd.DataFrame(pop)
I expected that the outer keys of the nested dictionary to be the column names and the inner keys to be the index names:
Note the interpreter syntax is iPython running on Jupyter Notebook Python 3
Nevada Ohio
2000 NaN 1.5
2001 2.4 1.7
2002 2.9 3.6
However, I continuously get the data with the keys arranged in the wrong order:
>> frame3
Nevada Ohio
2001 2.4 1.7
2002 2.9 3.6
2000 NaN 1.5
and I can't seem to make the indexes appear in the order that I desire.
Why is this happening? How can I rectify the matter?
The odd thing is, this is what appears in my Jupyter notebook:
but when using learnpython.org
's IDE, I get the following expected output:
Again, the same erroneous output is observed on my iPython:
Solution
You just need to put it in the data frame and sort it. Check this out :
pandas_dataframe = pd.DataFrame(pop).sort_index()
print(pandas_dataframe)
Out[128]:
Nevada Ohio
2000 NaN 1.5
2001 2.4 1.7
2002 2.9 3.6
Answered By - Nima
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.