How to Add the Value of the Same Key in Python

Asked 1 years ago, Updated 1 years ago, 243 views

There is a dictionary array called band, and one key has multiple values.
For each key (Band 1, Band 2, Band 3) in this array, I would like to calculate the sum of the values that the key has.Below is the image of the result you want to print.

BandNum : Total
Band 1 : 57468
Band 2 : 65377
Band 3 : 62070

I tried to calculate using the sum function, but I got the following error.

TypeError: unsupported operand type(s) for +: 'int' and 'list'

"I think the error means ""int and list use + operator"", but I don't know how to solve this problem."
Thank you for your understanding.

The code is shown below.

import pandas as pd

df = pd.read_fwf('Histogram1.txt', header=None, names=['Band', 'DN', 'Npts', 'Total', 'Percent', 'Acc Pct'])
band = df.query('Band != "Histogram"').ffill().astype({'DN': int, 'Npts': int, 'Total': int, 'Percent': float, 'Acc Pct': float})\
         .groupby('Band').apply(lambda x: x['DN'].repeat(x['Npts']).to_list()).to_dict()

print(band)

result = sum(band.values())

print(result)

Below is the first "Histogram1.txt" file to load.

Histogram       DN     Npts    Total   Percent   Acc Pct
Band 1         178        1        1    0.3322    0.3322
               179        2        3    0.6645    0.9967
               180        3        6    0.9967    1.9934
               181        6       12    1.9934    3.9867
               182        8       20    2.6578    6.6445
               183       10       30    3.3223    9.9668
               184       10       40    3.3223   13.2890
               185       11       51    3.6545   16.9435
               186       10       61    3.3223   20.2658
               187       21       82    6.9767   27.2425
               188       25      107    8.3056   35.5482
               189       26      133    8.6379   44.1860
               190       23      156    7.6412   51.8272
               191       20      176    6.6445   58.4718
               192       20      196    6.6445   65.1163
               193        9      205    2.9900   68.1063
               194        8      213    2.6578   70.7641
               195       13      226    4.3189   75.0831
               196       19      245    6.3123   81.3953
               197       11      256    3.6545   85.0498
               198       12      268    3.9867   89.0365
               199       12      280    3.9867   93.0233
               200        6      286    1.9934   95.0166
               201        3      289    0.9967   96.0133
               202        6      295    1.9934   98.0066
               203        4      299    1.3289   99.3355
               204        2      301    0.6645  100.0000

Histogram       DN     Npts    Total   Percent   Acc Pct
Band 2         208        1        1    0.3322    0.3322
               209        4        5    1.3289    1.6611
               210        9       14    2.9900    4.6512
               211       12       26    3.9867    8.6379
               212       28       54    9.3023   17.9402
               213       38       92   12.6246   30.5648
               214       11      103    3.6545   34.2193
               215       24      127    7.9734   42.1927
               216       24      151    7.9734   50.1661
               217       23      174    7.6412   57.8073
               218       12      186    3.9867   61.7940
               219       15      201    4.9834   66.7774
               220       16      217    5.3156   72.0930
               221       15      232    4.9834   77.0764
               222       18      250    5.9801   83.0565
               223       10      260    3.3223   86.3787
               224       17      277    5.6478   92.0266
               225       13      290    4.3189   96.3455
               226        4      294    1.3289   97.6744
               227        3      297    0.9967   98.6711
               228        4      301    1.3289  100.0000

Histogram       DN     Npts    Total   Percent   Acc Pct
Band 3         193        1        1    0.3322    0.3322
               194        3        4    0.9967    1.3289
               195        3        7    0.9967    2.3256
               196        7       14    2.3256    4.6512
               197        9       23    2.9900    7.6412
               198       17       40    5.6478   13.2890
               199        6       46    1.9934   15.2824
               200       12       58    3.9867   19.2691
               201       26       84    8.6379   27.9070
               202       20      104    6.6445   34.5515
               203       15      119    4.9834   39.5349
               204       15      134    4.9834   44.5183
               205       14      148    4.6512   49.1694
               206       16      164    5.3156   54.4850
               207       18      182    5.9801   60.4651
               208       14      196    4.6512   65.1163
               209       13      209    4.3189   69.4352
               210        8      217    2.6578   72.0930
               211       16      233    5.3156   77.4086
               212        7      240    2.3256   79.7342
               213       14      254    4.6512   84.3854
               214       16      270    5.3156   89.7010
               215        6      276    1.9934   91.6944
               216        4      280    1.3289   93.0233
               217        7      287    2.3256   95.3488
               218        2      289    0.6645   96.0133
               219        2      291    0.6645   96.6777
               220        3      294    0.9967   97.6744
               221        2      296    0.6645   98.3389
               222        0      296    0.0000   98.3389
               223        2      298    0.6645   99.0033
               224        3      301    0.9967  100.0000

The dictionary array band is shown below (some of them are omitted because they are long)

{'Band 1': [178, 179, 179, 180, 180, 180, 181, 181, 181, ...omitted..., 204, 204], 'Band 2': [208, 209, 209, 209, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210, ...omitted..., 228, 228, 228], 'Band 3': [193, 194, 196, 194, 194, 194, 194, 194, 196, 199, 199, 199, 199, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 194, 194, 194, 19

python

2023-03-29 13:51

1 Answers

import pandas as pd

df = pd.read_fwf('Histogram1.txt', header=None, names=['Band', 'DN', 'Npts', 'Total', 'Percent', 'Acc Pct'])
band = df.query('Band != "Histogram"').ffill().astype({'DN': int, 'Npts': int, 'Total': int, 'Percent': float, 'Acc Pct': float})\
         .groupby('Band').apply(lambda x: x['DN'].repeat(x['Npts']).to_list())

result = band.apply(sum).to_dict()
print(result)

# # {'Band 1': 57468, 'Band 2': 65377, 'Band 3': 62070}

##band = band.to_dict()
##print(band)


2023-03-29 19:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.