Posts: 45
Threads: 21
Joined: Dec 2016
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
Posts: 8,167
Threads: 160
Joined: Sep 2016
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
Posts: 45
Threads: 21
Joined: Dec 2016
(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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
In Python until v3.6 the dicts are unordered
Posts: 8,167
Threads: 160
Joined: Sep 2016
Jan-09-2017, 09:41 AM
(This post was last modified: Jan-09-2017, 09:41 AM by buran.)
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.
|