Python Forum

Full Version: print pandas's df
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
(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.
In Python until v3.6 the dicts are unordered
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.