Python Forum
print pandas's df - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: print pandas's df (/thread-1512.html)



print pandas's df - landlord1984 - Jan-09-2017

Here is my code:

import pandas as pd
web_stats={'Day':[1,2,3,4,5,6],
           'Visitor':[43,34,65,56,29,76],
           'Bounce Rate':[65,67,78,65,45,52]}
df=pd.DataFrame(web_stats)
print(df.head())

--------------
Output is:

   Bounce Rate  Day  Visitor
0           65    1       43
1           67    2       34
2           78    3       65
3           65    4       56
4           45    5       29
-------------------
My question is: Shouldn't it print column of Day and Visitor before Bounce Rate?

L


RE: print pandas's df - buran - Jan-09-2017

Please, use code tags in the future.
import pandas as pd
web_stats={'Day':[1,2,3,4,5,6],
           'Visitor':[43,34,65,56,29,76],
           'Bounce Rate':[65,67,78,65,45,52]}
df=pd.DataFrame(data=web_stats, columns=['Day', 'Visitor', 'Bounce Rate'])
print(df.head())
Output:
   Day  Visitor  Bounce Rate 0    1       43           65 1    2       34           67 2    3       65           78 3    4       56           65 4    5       29           45



RE: print pandas's df - landlord1984 - Jan-09-2017

(Jan-09-2017, 09:05 AM)buran Wrote: Please, use code tags in the future.
import pandas as pd web_stats={'Day':[1,2,3,4,5,6],            'Visitor':[43,34,65,56,29,76],            'Bounce Rate':[65,67,78,65,45,52]} df=pd.DataFrame(data=web_stats, columns=['Day', 'Visitor', 'Bounce Rate']) print(df.head())
Output:
Day Visitor Bounce Rate 0 1 43 65 1 2 34 67 2 3 65 78 3 4 56 65 4 5 29 45

Thanks.

I am curious why we have to specify the order in "df=pd.DataFrame(data=web_stats, columns=['Day', 'Visitor', 'Bounce Rate'])".

In C++ or Matlab, array data are ordered as we typed.


RE: print pandas's df - wavic - Jan-09-2017

In Python until v3.6 the dicts are unordered


RE: print pandas's df - buran - Jan-09-2017

web_stat is dict, i.e. unordered by design. unless specified DataFrame creates columns names from sorted dict keys. Of course if you prefer you can use OrderedDict from collections module.