1

I have a code to concat 2 dataframes with pandas and sum the totals. The problem is I don't want to sum 'Fecha'and 'hours' because as you can see in the example I have a row with all values of that and I want a empty row there!

df = pd.read_csv('a_AR.csv')
df1 = pd.read_csv('a_US.csv')

frames = [df1, df]
result = pd.concat(frames)
result = result.sort_values(by=['Fecha','hours'])

del result['eCPM']
del result['Importe_a_pagar_a_medio']

result.loc['Total']= result.sum()

result.to_csv('a_AR-US_Days_hours.csv', index=False)

os.remove('a_US.csv')
os.remove('a_AR.csv')

Example results:

Fecha,hours,impressions,revenue
22/01/2018,23hs,1666,0.73
22/01/2018,23hs,67,0.02
00hs00hs01hs01hs02hs02hs03hs03...,01/01/201801/01/201801/01/201801/01...,1733,0.75

1 Answer 1

1

You need difference for all columns without Fecha and hours columns for filtering:

cols = df.columns.difference(['Fecha','hours'])

Another more dynamic solution with select_dtypes for select all numeric columns:

cols = df.select_dtypes(np.number).columns

df.loc['Total']= df[cols].sum()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.