0

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

1
  • Yes. It does not exist probably. Maybe it's just written with whitespace somewhere? But without seeing the data I can only guess. You can see the column names of your df when using df_g.columns. Maybe you see whitespace there in the column name? Commented Nov 17, 2021 at 10:45

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply. I tried that: df_g['F'] = df_g.apply(lambda x: get_substr(string = x['EQ'], axis = 1, char= '-', n=1)), but it still not working. :-(
axis is argument for apply, not for get_substr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.