[英]Apply custom function to entire dataframe
I have a function which call another one.我有一个 function 调用另一个。
The objective is, by calling function get_substr to extract a substring based on a position of the nth occurence of a character目标是,通过调用 function get_substr 来提取基于字符第 n 次出现的 position 的 substring
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. function 有效。 Now I want to apply it on a dataframe by doing this.
现在我想通过这样做将它应用到 dataframe 上。
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.我不明白,因为 df_g['EQ'] 存在。 Can you help me?
你能帮助我吗? Thanks
谢谢
You forgot about axis=1
, without that function is applied to each column rather than each row .您忘记了
axis=1
,没有 function 应用于每一列而不是每一行。 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 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作为旁注,如果您使用单列中的值,则可以使用
pandas.Series.apply
而不是pandas.DataFrame.apply
,在上面的示例中
df['Z'] = df['A'].apply(lambda x:x*100)
in place of代替
df['Z'] = df.apply(lambda x:x['A']*100,axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.