Home > Mobile >  Stange Pandas df[column].sum() output
Stange Pandas df[column].sum() output

Time:03-30

I'm trying to sum up a df column like this:

import pandas as pd

data = [('name1', 2200, 2200), ('name2', 450, 450)]

column_list = ['name', '2021-01', '2021-02']

df = pd.DataFrame(data, columns=[column_list])

date1 = '2021-01'

total = df[date1].sum()

print(total)

The df looks like this:

    name 2021-01 2021-02
0  name1    2200    2200
1  name2     450     450

The output of print(total) looks like this:

2021-01    2650
dtype: int64

and I'm not quite sure why or how to just get the 2650?

Thanks so much for any help

CodePudding user response:

You should deliver flat list as columns rather than list of lists i.e.

import pandas as pd

data = [('name1', 2200, 2200), ('name2', 450, 450)]

column_list = ['name', '2021-01', '2021-02']

df = pd.DataFrame(data, columns=column_list)

date1 = '2021-01'

total = df[date1].sum()

print(total)

output

2650
  • Related