Python Forum

Full Version: Header above a list in Panda
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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')
(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]
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'})