Home > Mobile >  Apply custom function to entire dataframe
Apply custom function to entire dataframe

Time:11-18

I have a function which call another one.

The objective is, by calling function get_substr to extract a substring based on a position of the nth occurence of a character

def find_nth(string, char, n):
   start = string.find(char)
   while start >= 0 and n > 1:
      start = string.find(char, start len(char))
      n -= 1
return start
def get_substr(string,char,n):
   if n == 1:
      return string[0:find_nth(string,char,n)]
   else:
      return string[find_nth(string,char,n-1) len(char):find_nth(string,char,n)]  

The function works. Now I want to apply it on a dataframe by doing this.

df_g['F'] = df_g.apply(lambda x: get_substr(x['EQ'],'-',1))

I get on error:

KeyError: 'EQ'

I don't understand it as df_g['EQ'] exists. Can you help me? Thanks

CodePudding user response:

You forgot about axis=1, without that function is applied to each column rather than each row. Consider simple example

import pandas as pd
df = pd.DataFrame({'A':[1,2],'B':[3,4]})
df['Z'] = df.apply(lambda x:x['A']*100,axis=1)
print(df)

output

   A  B    Z
0  1  3  100
1  2  4  200

As side note if you are working with value from single column you might use pandas.Series.apply rather than pandas.DataFrame.apply, in above example it would mean

df['Z'] = df['A'].apply(lambda x:x*100)

in place of

df['Z'] = df.apply(lambda x:x['A']*100,axis=1)
  • Related