'Stange Pandas df[column].sum() output
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daweo |
