1
$\begingroup$

I created this script:

        import pandas as pd
        pd.set_option('display.min_rows', None)
        pd.set_option('display.max_columns', None)
        df = pd.read_excel('file.xlsx', sep=';', skiprows=6)
        df = df.drop(['Position', 'Swap'], axis=1)
        df.drop_duplicates(['Time'])
        df = df.groupby(['Symbol'])[['Profit', 'Volume', 'Commission']].sum()

        df['Comm. ratio'] = (df['Commission'] / df['Profit'])
        df['Comm. ratio'] = df['Comm. ratio'].round(2)
        df['Net profit'] = (df['Profit'] + df['Commission'])
        df.loc['Total'] = pd.Series(df['Commission'].sum(), index = ['Commission'])



    Symbol  Profit Volume Commission Comm. ratio Net profit

    BA
    FB
    NVDA
    US500
    USTEC
    Total                     -45



    df.loc['Total'] = pd.Series(df['Commission'].sum(), index = ['Commission'])

works on "commission" column, but I'd like to have a multiple column sum for "Profit, Commission, and Net profit" in the "Total" row.

I couldn't make it work.

Thanks!

$\endgroup$
1
$\begingroup$
df.loc['Total'] = pd.Series([df['Commission'].sum(),df['Profit'].sum(),df['Net profit'].sum()], index = ['Commission','Profit','Net profit'])
$\endgroup$
1
  • $\begingroup$ Thanks! I was merging commission, profit and net profit under the same .sum() $\endgroup$ – Steven Jul 5 '20 at 17:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.