Python Forum
Header above a list in Panda
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Header above a list in Panda
#1
I had just started with notebooks and Python. When I print my list will I just the list and then number how often they occur.

I would like to if I could get hour as a header and count as a header ,see pic

df = pd.read_csv('/Users/maria/Desktop/data.log', names=[0, 'hour', 2, 3], sep=':', engine='python')
x=df['hour'].value_counts()
print(x[0:2])
[Image: Screen_Shot_2018_08_11_at_18_29_44.png]

For the moment am I getting this below my results Name: hour, dtype: int64

/Maria
Reply
#2
values_count returns a Series, but you can convert it to a DataFrame object,e.g.

x = df['hour'].value_counts()
x_df = pd.DataFrame(x)
Finally, you can rename index column:

x_df = x_df.rename_axis('counts')
Reply
#3
(Aug-11-2018, 11:41 PM)scidam Wrote: values_count returns a Series, but you can convert it to a DataFrame object,e.g.

x = df['hour'].value_counts()
x_df = pd.DataFrame(x)
Finally, you can rename index column:

x_df = x_df.rename_axis('counts')

Thanks. But I still one problem. The hour heading is above count and vice versa, see pic.

How can I solve this?

[Image: Screen_Shot_2018_08_12_at_09_59_15.png]
Reply
#4
This is because counts is an index column, try the following instead:

counts = =pd.DataFrame(df.hour.value_counts().rename('counts')).reset_index().rename(columns={'index': 'hour'})
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Panda pivot_table questions dee 1 741 Jan-09-2023, 05:45 PM
Last Post: dee
  Remove values for weekend in a panda series JaneTan 0 680 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  panda table data kucingkembar 0 1,132 Mar-01-2022, 10:38 PM
Last Post: kucingkembar
  can we write command output to new csv file using Panda package? PythonBeginner_2020 3 2,393 Mar-13-2020, 12:38 PM
Last Post: ndc85430
  Creating csv header from user-input list dvanommen 2 2,160 Aug-26-2019, 08:51 PM
Last Post: dvanommen

Forum Jump:

User Panel Messages

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