Вы не можете sum
строк против целых чисел.
Анализируйте ваши ''
записи как np.nan
или просто 0
, а затем делайте сумму.
>>> import pandas as pd
>>> df = pd.DataFrame([])
>>> df = df.append([['Variable A','',435.65]], ignore_index = True)
>>> df = df.append([['Variable B',324,'']], ignore_index = True)
>>> df = df.append([['Variable C','',300]], ignore_index = True)
>>> df.columns = ['Type','DR','CR']
>>>
>>>
>>> df
Type DR CR
0 Variable A 435.65
1 Variable B 324
2 Variable C 300
>>>>>> import numpy as np
>>> df['DR'] = np.where(df['DR'] == '', [0],df['DR'])
>>> df
Type DR CR
0 Variable A 0 435.65
1 Variable B 324
2 Variable C 0 300
>>> print('TOTAL DR ',df['DR'].sum())
TOTAL DR 324
Другой вариант - использовать pd.to_numeric
перед суммированием.
>>> df = pd.DataFrame([])
>>> df = df.append([['Variable A','',435.65]], ignore_index = True)
>>> df = df.append([['Variable B',324,'']], ignore_index = True)
>>> df = df.append([['Variable C','',300]], ignore_index = True)
>>> df.columns = ['Type','DR','CR']
>>> print('TOTAL DR ',pd.to_numeric(df['DR']).sum())
TOTAL DR 324.0
>>> print('TOTAL DR ',pd.to_numeric(df['CR']).sum())
TOTAL DR 735.65