Applying function on non-null values of two columns in a data frame
$begingroup$
I am trying to apply a function on a data frame, the function calculates fuzzy matching and Jellyfish features on two columns. I want this function to do the calculation only on non-null values, even if there is a null in one of the columns.
def feat1(df, col1, col2):
features = [hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio]
for j in features:
df[str.split(col1,sep='_')[0]+'_'+str.split(j,)] =
df[[col1,col2]].apply(
lambda row: j(row[col1], row[col2])
if (pd.notnull(row[col1]) & pd.notnull(row[col2]))
else np.NaN,axis=1)
But I am getting the following error:
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
What's the cause of the error?
python dataframe
$endgroup$
add a comment |
$begingroup$
I am trying to apply a function on a data frame, the function calculates fuzzy matching and Jellyfish features on two columns. I want this function to do the calculation only on non-null values, even if there is a null in one of the columns.
def feat1(df, col1, col2):
features = [hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio]
for j in features:
df[str.split(col1,sep='_')[0]+'_'+str.split(j,)] =
df[[col1,col2]].apply(
lambda row: j(row[col1], row[col2])
if (pd.notnull(row[col1]) & pd.notnull(row[col2]))
else np.NaN,axis=1)
But I am getting the following error:
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
What's the cause of the error?
python dataframe
$endgroup$
1
$begingroup$
This is probably more suited to StackOverflow. Anyway, I think the problem is thestr.split(j,)
:j
is a function,(j,)
is an iterable with only a function as a member, andstr.split
expects an iterable ofstr
. You could adapt your code to have pairs of(distance_function, name)
:features = [(hamming_distance, 'Hamming distance'), (jaro_winkler, 'Jaro-Winkler'), ...]
.
$endgroup$
– Mephy
2 days ago
add a comment |
$begingroup$
I am trying to apply a function on a data frame, the function calculates fuzzy matching and Jellyfish features on two columns. I want this function to do the calculation only on non-null values, even if there is a null in one of the columns.
def feat1(df, col1, col2):
features = [hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio]
for j in features:
df[str.split(col1,sep='_')[0]+'_'+str.split(j,)] =
df[[col1,col2]].apply(
lambda row: j(row[col1], row[col2])
if (pd.notnull(row[col1]) & pd.notnull(row[col2]))
else np.NaN,axis=1)
But I am getting the following error:
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
What's the cause of the error?
python dataframe
$endgroup$
I am trying to apply a function on a data frame, the function calculates fuzzy matching and Jellyfish features on two columns. I want this function to do the calculation only on non-null values, even if there is a null in one of the columns.
def feat1(df, col1, col2):
features = [hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio]
for j in features:
df[str.split(col1,sep='_')[0]+'_'+str.split(j,)] =
df[[col1,col2]].apply(
lambda row: j(row[col1], row[col2])
if (pd.notnull(row[col1]) & pd.notnull(row[col2]))
else np.NaN,axis=1)
But I am getting the following error:
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
TypeError Traceback (most recent call last) in ----> 1 feat1(dfCart,'FirstName_x','FirstName_y')
in feat1(df, col1, col2) 2 features=[hamming_distance,jaro_winkler,damerau_levenshtein_distance,ratio,partial_ratio,partial_token_set_ratio,partial_token_sort_ratio] 3 for j in features: ----> 4 df[str.split(col1,sep='')[0]+''+str.split(j,)]=df[[col1,col2]].apply(lambda row: j(row[col1],row[col2]) if (pd.notnull(row[col1]) & pd.notnull(row[col2])) else np.NaN,axis=1)
TypeError: descriptor 'split' requires a 'str' object but received a 'builtin_function_or_method'
What's the cause of the error?
python dataframe
python dataframe
edited yesterday
Mephy
683418
683418
asked 2 days ago
GNJGNJ
1
1
1
$begingroup$
This is probably more suited to StackOverflow. Anyway, I think the problem is thestr.split(j,)
:j
is a function,(j,)
is an iterable with only a function as a member, andstr.split
expects an iterable ofstr
. You could adapt your code to have pairs of(distance_function, name)
:features = [(hamming_distance, 'Hamming distance'), (jaro_winkler, 'Jaro-Winkler'), ...]
.
$endgroup$
– Mephy
2 days ago
add a comment |
1
$begingroup$
This is probably more suited to StackOverflow. Anyway, I think the problem is thestr.split(j,)
:j
is a function,(j,)
is an iterable with only a function as a member, andstr.split
expects an iterable ofstr
. You could adapt your code to have pairs of(distance_function, name)
:features = [(hamming_distance, 'Hamming distance'), (jaro_winkler, 'Jaro-Winkler'), ...]
.
$endgroup$
– Mephy
2 days ago
1
1
$begingroup$
This is probably more suited to StackOverflow. Anyway, I think the problem is the
str.split(j,)
: j
is a function, (j,)
is an iterable with only a function as a member, and str.split
expects an iterable of str
. You could adapt your code to have pairs of (distance_function, name)
: features = [(hamming_distance, 'Hamming distance'), (jaro_winkler, 'Jaro-Winkler'), ...]
.$endgroup$
– Mephy
2 days ago
$begingroup$
This is probably more suited to StackOverflow. Anyway, I think the problem is the
str.split(j,)
: j
is a function, (j,)
is an iterable with only a function as a member, and str.split
expects an iterable of str
. You could adapt your code to have pairs of (distance_function, name)
: features = [(hamming_distance, 'Hamming distance'), (jaro_winkler, 'Jaro-Winkler'), ...]
.$endgroup$
– Mephy
2 days ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46928%2fapplying-function-on-non-null-values-of-two-columns-in-a-data-frame%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f46928%2fapplying-function-on-non-null-values-of-two-columns-in-a-data-frame%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
This is probably more suited to StackOverflow. Anyway, I think the problem is the
str.split(j,)
:j
is a function,(j,)
is an iterable with only a function as a member, andstr.split
expects an iterable ofstr
. You could adapt your code to have pairs of(distance_function, name)
:features = [(hamming_distance, 'Hamming distance'), (jaro_winkler, 'Jaro-Winkler'), ...]
.$endgroup$
– Mephy
2 days ago