How to use one column to cover another column and adopt another column value when NaN?How do I sort a dictionary by value?Add one row to pandas DataFrameAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeCreating an empty Pandas DataFrame, then filling it?Set value for particular cell in pandas DataFrame using index“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
Hostile Divisor Numbers
How to properly store the current value of int variable into a token list?
Page count conversion from single to double-space for submissions
What happens if I accidentally leave an app running and click "Install Now" in Software Updater?
What do you call a painting on a wall?
What is the closest airport to the center of the city it serves?
It isn’t that you must stop now
Why are oscilloscope input impedances so low?
Why is "breaking the mould" positively connoted?
Sheared off exhasut pipe: How to fix without a welder?
Why is my arithmetic with a long long int behaving this way?
As a GM, is it bad form to ask for a moment to think when improvising?
Is 'contemporary' ambiguous and if so is there a better word?
How to calculate rate of axial precession?
My large rocket is still flipping over
Has the Hulk always been able to talk?
Dangerous workplace travelling
How to pass query parameters in URL in Salesforce Summer 19 Release?
How to remap repeating commands i.e. <number><command>?
Is disk brake effectiveness mitigated by tyres losing traction under strong braking?
Dirichlet series with a single zero
How to preserve a rare version of a book?
Simple Derivative Proof?
Would a "Permanence" spell in 5e be overpowered?
How to use one column to cover another column and adopt another column value when NaN?
How do I sort a dictionary by value?Add one row to pandas DataFrameAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeCreating an empty Pandas DataFrame, then filling it?Set value for particular cell in pandas DataFrame using index“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
My system is Python3.6 with pandas 0.24
There are 2 column cola
and colb
in my dataframe df
as below:
cola colb
A C
C NaN
NaN C
C D
I want to use colb
to cover cola
if both has row value,adopt cola
if colb
has no value.
The expected result as below:
cola colb colc
A C C
C NaN C
NaN C C
C D D
How to do it?
Thanks in advance!
python pandas dataframe
add a comment |
My system is Python3.6 with pandas 0.24
There are 2 column cola
and colb
in my dataframe df
as below:
cola colb
A C
C NaN
NaN C
C D
I want to use colb
to cover cola
if both has row value,adopt cola
if colb
has no value.
The expected result as below:
cola colb colc
A C C
C NaN C
NaN C C
C D D
How to do it?
Thanks in advance!
python pandas dataframe
add a comment |
My system is Python3.6 with pandas 0.24
There are 2 column cola
and colb
in my dataframe df
as below:
cola colb
A C
C NaN
NaN C
C D
I want to use colb
to cover cola
if both has row value,adopt cola
if colb
has no value.
The expected result as below:
cola colb colc
A C C
C NaN C
NaN C C
C D D
How to do it?
Thanks in advance!
python pandas dataframe
My system is Python3.6 with pandas 0.24
There are 2 column cola
and colb
in my dataframe df
as below:
cola colb
A C
C NaN
NaN C
C D
I want to use colb
to cover cola
if both has row value,adopt cola
if colb
has no value.
The expected result as below:
cola colb colc
A C C
C NaN C
NaN C C
C D D
How to do it?
Thanks in advance!
python pandas dataframe
python pandas dataframe
edited Apr 3 at 13:26
Scott Boston
60k73258
60k73258
asked Mar 23 at 3:07
kittygirlkittygirl
590617
590617
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This is more like fillna
with pd.Series
df.colb.fillna(df.cola)
Out[593]:
0 C
1 C
2 C
3 D
Name: colb, dtype: object
#df['colc']=df.colb.fillna(df.cola)
add a comment |
You can use np.where
df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])
print(df)
Output:
cola colb colc
0 A C C
1 C None C
2 None C C
3 C D D
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f55310233%2fhow-to-use-one-column-to-cover-another-column-and-adopt-another-column-value-whe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is more like fillna
with pd.Series
df.colb.fillna(df.cola)
Out[593]:
0 C
1 C
2 C
3 D
Name: colb, dtype: object
#df['colc']=df.colb.fillna(df.cola)
add a comment |
This is more like fillna
with pd.Series
df.colb.fillna(df.cola)
Out[593]:
0 C
1 C
2 C
3 D
Name: colb, dtype: object
#df['colc']=df.colb.fillna(df.cola)
add a comment |
This is more like fillna
with pd.Series
df.colb.fillna(df.cola)
Out[593]:
0 C
1 C
2 C
3 D
Name: colb, dtype: object
#df['colc']=df.colb.fillna(df.cola)
This is more like fillna
with pd.Series
df.colb.fillna(df.cola)
Out[593]:
0 C
1 C
2 C
3 D
Name: colb, dtype: object
#df['colc']=df.colb.fillna(df.cola)
answered Mar 23 at 3:48
Wen-BenWen-Ben
131k83972
131k83972
add a comment |
add a comment |
You can use np.where
df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])
print(df)
Output:
cola colb colc
0 A C C
1 C None C
2 None C C
3 C D D
add a comment |
You can use np.where
df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])
print(df)
Output:
cola colb colc
0 A C C
1 C None C
2 None C C
3 C D D
add a comment |
You can use np.where
df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])
print(df)
Output:
cola colb colc
0 A C C
1 C None C
2 None C C
3 C D D
You can use np.where
df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])
print(df)
Output:
cola colb colc
0 A C C
1 C None C
2 None C C
3 C D D
answered Mar 23 at 4:05
AkshayNevrekarAkshayNevrekar
6,419102243
6,419102243
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f55310233%2fhow-to-use-one-column-to-cover-another-column-and-adopt-another-column-value-whe%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