Python Forum
Header above a list in Panda - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Header above a list in Panda (/thread-12144.html)



Header above a list in Panda - sweet_swiss - Aug-11-2018

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


RE: Header above a list in Panda - scidam - Aug-11-2018

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')



RE: Header above a list in Panda - sweet_swiss - Aug-12-2018

(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]


RE: Header above a list in Panda - scidam - Aug-12-2018

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'})