How to fill missing value based on other columns in Pandas dataframe?
$begingroup$
Suppose I have a 5*3 data frame in which third column contains missing value
1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN
I hope to generate value for missing value based rule that first product second column
1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6
How can I do it use data frame? Thanks.
How to add condition to calculate missing value like this?
if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd
1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1
pandas
$endgroup$
add a comment |
$begingroup$
Suppose I have a 5*3 data frame in which third column contains missing value
1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN
I hope to generate value for missing value based rule that first product second column
1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6
How can I do it use data frame? Thanks.
How to add condition to calculate missing value like this?
if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd
1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1
pandas
$endgroup$
$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53
$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25
add a comment |
$begingroup$
Suppose I have a 5*3 data frame in which third column contains missing value
1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN
I hope to generate value for missing value based rule that first product second column
1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6
How can I do it use data frame? Thanks.
How to add condition to calculate missing value like this?
if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd
1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1
pandas
$endgroup$
Suppose I have a 5*3 data frame in which third column contains missing value
1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN
I hope to generate value for missing value based rule that first product second column
1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6
How can I do it use data frame? Thanks.
How to add condition to calculate missing value like this?
if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd
1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1
pandas
pandas
edited Mar 22 '17 at 13:10
KyL
asked Mar 22 '17 at 12:57
KyLKyL
149114
149114
$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53
$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25
add a comment |
$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53
$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25
$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53
$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53
$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25
$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
Assuming three columns of your dataframe is a
, b
and c
. This is what you want:
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
Full code:
df = pd.DataFrame(
np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
columns=['a', 'b', 'c']
)
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
$endgroup$
add a comment |
$begingroup$
Assuming that the three columns in your dataframe are a
, b
and c
. Then you can do the required operation like this:
values = df['a'] * df['b']
df['c'] = values.where(df['c'] == np.nan, others=df['c'])
$endgroup$
1
$begingroup$
Ornp.where(pd.isnull(df.c), df.a * df.b, df.c)
$endgroup$
– Valentas
Apr 16 '18 at 7:18
add a comment |
$begingroup$
Another option:
df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B
$endgroup$
add a comment |
$begingroup$
hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"
New contributor
$endgroup$
add a comment |
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%2f17769%2fhow-to-fill-missing-value-based-on-other-columns-in-pandas-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Assuming three columns of your dataframe is a
, b
and c
. This is what you want:
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
Full code:
df = pd.DataFrame(
np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
columns=['a', 'b', 'c']
)
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
$endgroup$
add a comment |
$begingroup$
Assuming three columns of your dataframe is a
, b
and c
. This is what you want:
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
Full code:
df = pd.DataFrame(
np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
columns=['a', 'b', 'c']
)
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
$endgroup$
add a comment |
$begingroup$
Assuming three columns of your dataframe is a
, b
and c
. This is what you want:
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
Full code:
df = pd.DataFrame(
np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
columns=['a', 'b', 'c']
)
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
$endgroup$
Assuming three columns of your dataframe is a
, b
and c
. This is what you want:
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
Full code:
df = pd.DataFrame(
np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
columns=['a', 'b', 'c']
)
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
answered Mar 22 '17 at 13:15
IcybladeIcyblade
2,5111333
2,5111333
add a comment |
add a comment |
$begingroup$
Assuming that the three columns in your dataframe are a
, b
and c
. Then you can do the required operation like this:
values = df['a'] * df['b']
df['c'] = values.where(df['c'] == np.nan, others=df['c'])
$endgroup$
1
$begingroup$
Ornp.where(pd.isnull(df.c), df.a * df.b, df.c)
$endgroup$
– Valentas
Apr 16 '18 at 7:18
add a comment |
$begingroup$
Assuming that the three columns in your dataframe are a
, b
and c
. Then you can do the required operation like this:
values = df['a'] * df['b']
df['c'] = values.where(df['c'] == np.nan, others=df['c'])
$endgroup$
1
$begingroup$
Ornp.where(pd.isnull(df.c), df.a * df.b, df.c)
$endgroup$
– Valentas
Apr 16 '18 at 7:18
add a comment |
$begingroup$
Assuming that the three columns in your dataframe are a
, b
and c
. Then you can do the required operation like this:
values = df['a'] * df['b']
df['c'] = values.where(df['c'] == np.nan, others=df['c'])
$endgroup$
Assuming that the three columns in your dataframe are a
, b
and c
. Then you can do the required operation like this:
values = df['a'] * df['b']
df['c'] = values.where(df['c'] == np.nan, others=df['c'])
answered Mar 22 '17 at 13:45
NainNain
1,37951830
1,37951830
1
$begingroup$
Ornp.where(pd.isnull(df.c), df.a * df.b, df.c)
$endgroup$
– Valentas
Apr 16 '18 at 7:18
add a comment |
1
$begingroup$
Ornp.where(pd.isnull(df.c), df.a * df.b, df.c)
$endgroup$
– Valentas
Apr 16 '18 at 7:18
1
1
$begingroup$
Or
np.where(pd.isnull(df.c), df.a * df.b, df.c)
$endgroup$
– Valentas
Apr 16 '18 at 7:18
$begingroup$
Or
np.where(pd.isnull(df.c), df.a * df.b, df.c)
$endgroup$
– Valentas
Apr 16 '18 at 7:18
add a comment |
$begingroup$
Another option:
df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B
$endgroup$
add a comment |
$begingroup$
Another option:
df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B
$endgroup$
add a comment |
$begingroup$
Another option:
df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B
$endgroup$
Another option:
df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B
answered Jun 4 '18 at 19:24
VishalVishal
1634
1634
add a comment |
add a comment |
$begingroup$
hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"
New contributor
$endgroup$
add a comment |
$begingroup$
hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"
New contributor
$endgroup$
add a comment |
$begingroup$
hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"
New contributor
$endgroup$
hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"
New contributor
New contributor
answered 6 mins ago
user7389747user7389747
1
1
New contributor
New contributor
add a comment |
add a comment |
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%2f17769%2fhow-to-fill-missing-value-based-on-other-columns-in-pandas-dataframe%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
$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53
$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25