Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print pandas's df
#1
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
Reply
#2
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
Reply
#3
(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.
Reply
#4
In Python until v3.6 the dicts are unordered
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020