How to add multiple columns and assigning into new column name by using function in pandas dataframe?

user11386089

I have a data frame which has 50 columns. I would like to sum 10 columns and store the same into new column name (called savings to be created). I have 10 files, hence want to use function and apply the same to all the files.

Right now I am using iloc and it's working fine. I don't know how to bring this into a function?

my dataframe name called df.

df['savings'] = df.iloc[:,11:27].sum(axis = 1).round(2)

I want to use function and checked the below. But it is not working


def saving(x):
    x = df.iloc[:,11:27].sum(axis = 1).round(2)
    return(x)

df['savings] = df.applymap(x)

This is not working. Any suggestion. I have 10 different files and want to do the same sum.

jezrael

I believe you need pass DataFrame to function:

def saving(x):
    x['savings'] = x.iloc[:,11:27].sum(axis = 1).round(2)
    return x

If need apply function to DataFrame use DataFrame.pipe:

df = df.pipe(saving)

Or:

df = saving(df)

Loop by files, create DataFrames and apply function if need apply function to multiple DataFrames:

import glob

for f in glob.glob('files/*.csv'):
    df = pd.read_csv(f).pipe(saving)
    print (df) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas dataframe, how can I group by multiple columns and apply sum for specific column and add new count column?

how to add a new column on dataframe using a function

Applying function to multiple columns in pandas dataframe to make 1 new column

Spark Dataframe, add new column with function using other columns

Pandas DataFrame - How to apply Lambda Function on multiple columns and create a new column

How to add/insert output of a function call that returns multiple fields, as new columns into Pandas dataframe?

How to Split the Contents of a Single Pandas Dataframe Column into Multiple New Columns

Using pandas dataframe, how to group by multiple columns and adding new column with missing data

Pandas: Add new column and assigning value from another dataframe by condition

How to create new columns in pandas dataframe using column values?

Add Multiple Columns to Pandas Dataframe from Function

Applying a function to pandas dataframe and add a new column?

How to transpose and Pandas DataFrame and name new columns?

How to add multiple columns to a dataframe using a function returning a serie

Create new column into dataframe based on values from other columns using apply function onto multiple columns

Add a new column to a dataframe based on multiple columns from another dataframe

Pandas reindex and assigning Columns to a new column

How to add multilevel column name to specific column only(not all the columns) in python pandas.DataFrame?

Pandas dataframe, how can I group by single column and apply sum to multiple column and add new sum column?

Pandas: Assigning multiple *new* columns simultaneously

How do I add two new columns on the basis of the values of multiple other columns in a pandas dataframe?

How do I split data out from one column of a pandas dataframe into multiple columns of a new dataframe

How to add column name to cell in pandas dataframe?

How to add column name for index in Pandas dataframe

Pandas - how to add multiple conditional columns to dataframe?

How to add values to a new column in pandas dataframe?

Add new columns iteratively to a dataframe with a unique column name

How do I pivot my dataframe multiple times in pandas while creating a new column merging multiple columns?

Add data to a new column in pandas.DataFrame from existing columns using for-loop