来自不同数据框的总和值

Sum value 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

为了获得这个系列,我这样做了:

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'])

如何对每个值求和?如果我做 slotMorningSum['tempo'].sum() 我在输出中获得的是:

1193.036
540.226
1130.245
702.2320000000001
815.678
864.3360000000001
880.07
837.018
986.842
1080.2869999999998
1086.674

与之前获得的值相同(我也尝试使用 for 循环,但输出仍然相同)。我如何求和这个值?

根据您的代码:

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())