Home > Enterprise >  Sum value from different dataframe
Sum value from different dataframe

Time:02-22

i've got this series which come from from different dataframe:

10    1193.036
Name: tempo, dtype: float64
8    540.226
Name: tempo, dtype: float64
9    1130.245
Name: tempo, dtype: float64
7    702.232
Name: tempo, dtype: float64
9    815.678
Name: tempo, dtype: float64
2    864.336
Name: tempo, dtype: float64
3    880.07
Name: tempo, dtype: float64
2    837.018
Name: tempo, dtype: float64
2    986.842
Name: tempo, dtype: float64
1    1080.287
Name: tempo, dtype: float64
1    1086.674
Name: tempo, dtype: float64

To obtain this series I've done this:

path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))

for f in csv_files:
    dfDay = pd.read_csv(f, encoding = "ISO-8859-1", sep = ';')
    dfSlotSum = dfDay.groupby('slot', as_index=False)['tempo'].sum()
    slotMorningSum = dfSlotSum[dfSlotSum.slot == '7--8']
     
    if slotMorningSum.empty:
        continue   
   
    print(slotMorningSum['tempo'])

How can i sum every single value? if i do slotMorningSum['tempo'].sum() what I've obtain in output is:

1193.036
540.226
1130.245
702.2320000000001
815.678
864.3360000000001
880.07
837.018
986.842
1080.2869999999998
1086.674

that are same value obtained before(I also tried with for loop but the output it's still the same). How can i sum this value?

CodePudding user response:

Based on your code:

path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))

#What's missing here is a way to collect things up, one way is to create a `List` variable before the for loop,
list_ = []

for f in csv_files:
    dfDay = pd.read_csv(f, encoding = "ISO-8859-1", sep = ';')
    dfSlotSum = dfDay.groupby('slot', as_index=False)['tempo'].sum()
    slotMorningSum = dfSlotSum[dfSlotSum.slot == '7--8']
     
    if slotMorningSum.empty:
        continue 

    #then in this for loop you append all those slotMorningSum['tempo'] into the List
    list_.append(slotMorningSum['tempo'])

    print(slotMorningSum['tempo'])

#lastly, you combine them using pd.concat and do the sum()
concate = pd.concat(list_)
print(concate.sum())
  • Related