Python pandas - headers on two lines

Using the pandas library for python, I read the csv and then group the results with the sum.

grouped = df[['Organization Name','Views']].groupby('Organization Name').sum().sort(columns='Views',ascending=False).head(10)
#Bar Chart Section
print grouped.to_string()

      

Unfortunately, for a table, I get the following output:

                                      Views
Organization Name
Test1                                 112
Test2                                 114
Test3                                 115

      

it seems like the column headers are on two separate lines.

+3


source to share


2 answers


Since you are grouped under "Organization Name" this is used as the name for your index, you can set it to None

using:

grouped.index.name = None

      

Then will remove the line, this is just a display problem, your data is not in some funny shape



Alternatively, if you don't want the "organization name" to become an index, go as_index=False

to groupby

:

grouped = df[['Organization Name','Views']].groupby('Organization Name', as_index=False).sum().sort(columns='Views',ascending=False).head(10)

      

+3


source


grouped.reset_index()

should fix this. This is because you have grouped the data and aggregated it in a column.



0


source







All Articles