0

My original data has 3 columns - country, downloads and cost in xlsx file.

df = pd.read_excel(xls)

I want to show for each country the total downloads and total cost, in a table that looks good.

I tried using tabulate:

installs_by_country = df.groupby('Country'['downloads'].sum().to_frame(name='downloads').reset_index()
cost_by_country = df.groupby('Country')['Cost'].sum().to_frame(name='Cost').reset_index()
total_per_country = pd.DataFrame({'Country': [installs_by_country['Country']], 'downloads':[installs_by_country['downloads']], 'Cost': [cost_by_country['Cost']]})
print(tabulate(total_per_country, headers=['Country','downloads','Cost'], tablefmt='psql', showindex=False))

But the table appears to have indexes in each column:

Output result

How can I fix this? Please help

AmySQL
  • 1
  • I think you've got one too many sets of `[]` making a column of Series objects not a new DataFrame. `total_per_country = pd.DataFrame({'Country': installs_by_country['Country'], 'downloads': installs_by_country['downloads'], 'Cost': cost_by_country['Cost']})` or maybe `total_per_country = pd.concat([installs_by_country['Country'], installs_by_country['downloads'], cost_by_country['Cost']], axis=1)`? – Henry Ecker Nov 20 '21 at 21:31
  • 1
    Yes! It worked. the 1st one was the problem indeed thank you – AmySQL Nov 20 '21 at 21:35

0 Answers0