1

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.

Netwave
  • 40,134
  • 6
  • 50
  • 93

1 Answers1

0

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) 
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • def saving(x): x['sav'] = x.iloc[:,11:27].sum(axis = 1).round(2) return x df['sav'] = df.apply(saving) .. It's not working.. Getting error message""'int' object has no attribute 'iloc'", 'occurred at index nohhold')" –  Apr 22 '19 at 05:45
  • @sdh2000 - no, you need `df = df.pipe(saving)` instead `df['sav'] = df.apply(saving) ` – jezrael Apr 22 '19 at 05:47
  • def saving(x): x['savings'] = x.iloc[:,11:27].sum(axis = 1).round(2) return x df= df.pipe(saving) I am getting error "Permission denied" –  Apr 22 '19 at 05:50
  • @sdh2000 - and `df = saving(df)` ? – jezrael Apr 22 '19 at 05:51
  • @sdh2000 - hmmm, not problem with [this](https://stackoverflow.com/questions/50083806/permissionerror-permission-denied-to-reading-csv-file-in-python) ? – jezrael Apr 22 '19 at 05:53
  • it's not working.. what would be the reason.. again getting permission denied –  Apr 22 '19 at 06:03
  • @sdh2000 What is full error ? There is no permission denied for some file? – jezrael Apr 22 '19 at 06:04
  • PermissionError: [Errno 13] Permission denied –  Apr 22 '19 at 07:05
  • @sdh2000 - no idea what should be problem, if not problem with files. – jezrael Apr 22 '19 at 07:09
  • is there any other method to this function? –  Apr 22 '19 at 07:10
  • @sdh2000 - Is possible see your full script? – jezrael Apr 22 '19 at 07:10
  • It' s working now. I just restarted by the jupyter notebook. But the all values in the savings becomes 0. Sum is not working in the function. –  Apr 22 '19 at 07:18
  • its coming 0.0... Now I applied lamda and getting error "("() got an unexpected keyword argument 'errors'", 'occurred at index regio')" –  Apr 22 '19 at 07:21
  • @sdh2000 - oops, typo, need `x.iloc[:,11:27].apply(lambda x: pd.to_numeric(x, errors='coerce')).sum(axis=1)` – jezrael Apr 22 '19 at 07:28