Home > Net >  How to write a function with DataFrame in pandas/Python?
How to write a function with DataFrame in pandas/Python?

Time:12-09

Supposed we have a df with a sum() value in the below DataFrame, thanks so much for @jezrael 's answer here, but we have many different df like below DataFrame with different columns, is it possible to add those three line code in a function?

df.columns=['value_a','value_b','name','up_or_down','difference']
# from here
df.loc['sum'] = df[['value_a','value_b','difference']].sum()
df1 = df[['value_a','value_b','difference']].sum().to_frame().T
df = pd.concat([df1, df], ignore_index=True)
# end here
df

                                 value_a               value_b       name               up_or_down   difference
project_name
   sum                               27.56               25.04                                        -1.31     
2021-project11                      0.43               0.48        2021-project11            up        0.05
2021-project1                        0.62               0.56          2021-project1            down       -0.06
2021-project2                       0.51               0.47       2021-project2           down       -0.04
2021-porject3                       0.37               0.34         2021-porject3            down       -0.03
2021-porject4                       0.64               0.61         2021-porject4            down       -0.03
2021-project5                        0.32               0.25          2021-project5            down       -0.07
2021-project6                       0.75               0.81         2021-project6            up        0.06
2021-project7                      0.60               0.60        2021-project7            down        0.00
2021-project8                       0.85               0.74         2021-project8            down       -0.11
2021-project10                      0.67               0.67        2021-project10            down        0.00
2021-project9                     0.73               0.73        2021-project9           down        0.00
2021-project11                   0.54               0.54      2021-project11           down        0.00
2021-project12                 0.40               0.40    2021-project12           down        0.00
2021-project13                      0.76               0.77        2021-project13            up        0.01
2021-project14                     1.16               1.28        2021-project14           up        0.12
2021-project15                     1.01               0.94        2021-project15           down       -0.07
2021-project16                      1.23               1.24        2021-project16            up        0.01
2022-project17                       0.40               0.36          2022-project17           down       -0.04
2022-project_11                      0.40               0.40        2022-project_11            down        0.00
2022-project4                        1.01               0.80          2022-project4            down       -0.21
2022-project1                        0.65               0.67          2022-project1            up        0.02
2022-project2                    0.75               0.57       2022-project2           down       -0.18
2022-porject3                       0.32               0.32         2022-porject3            down        0.00
2022-project18                       0.91               0.56         2022-project18            down       -0.35
2022-project5                        0.84               0.89          2022-project5            up        0.05
2022-project19                      0.61               0.48        2022-project19            down       -0.13
2022-project6                       0.77               0.80         2022-project6            up        0.03
2022-project20                      0.63               0.54         2022-project20           down       -0.09
2022-project8                       0.59               0.55         2022-project8            down       -0.04
2022-project21                       0.58               0.54         2022-project21            down       -0.04
2022-project10                      0.76               0.76        2022-project10            down        0.00
2022-project9                     0.70               0.71        2022-project9           up        0.01
2022-project22                    0.62               0.56      2022-project22            down       -0.06
2022-project23               2.03               1.74  2022-project23           down       -0.29
2022-project12                 0.39               0.39    2022-project12           down        0.00
2022-project24                      1.35               1.55         2022-project24           up        0.20
project25                         0.45               0.42           project25            down       -0.03
project26                         0.53                NaN         project26              down         NaN
project27                           0.68                NaN           project27              down         NaN

Can I add a function with conditions like below, and our other df values can use the function directly?

def sum_handler(x):
    if .......
        return .....
    elif .......
        return .....
    else
        return .....

Thanks so much for any advice

CodePudding user response:

You could try a different approach for summing up your dataframe like shown in this answer.

df.loc['Total'] = df.sum(numeric_only=True, axis=0)

Since this is a one line of code, there would be no need to create a custom function to do this. But for future referrence, you can create a custom function and apply it to a dataframe like this:

import pandas as pd

def double_columns(df: pd.DataFrame, columns: list[str]):
    """ Doubles chosen columns of a dataframe """
    df[columns] = df[columns] * 2
    return df

df = pd.DataFrame({'col1': [1,2], 'col2': [2,3]})
df = double_columns(df, ['col1'])
print(df)

would return

   col1  col2
0     2     2
1     4     3
  • Related