Update Column A where Column B has Value C in Pandas DataFrame
Good morning,
I'm trying to update a DataFrame based on the contents of two columns and am running into issues.
Specifically, I have a column called IP
, another called VISITTIME
. I've added two columns called OLDEST
and NEWEST
which need to contain the min and max VISITTIME
for the IP
of that row.
Using:
df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(),
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")
I can get the min and max times for each IP
in the table. I can then iterate over that but I don't know how to update the original DataFrame
.
Essentially what I'm asking is how do I do the following in pandas
:
UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP;
I feel like this should be easy and I'm ashamed that it isn't.
Thank you
python pandas dataframe
add a comment |
Good morning,
I'm trying to update a DataFrame based on the contents of two columns and am running into issues.
Specifically, I have a column called IP
, another called VISITTIME
. I've added two columns called OLDEST
and NEWEST
which need to contain the min and max VISITTIME
for the IP
of that row.
Using:
df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(),
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")
I can get the min and max times for each IP
in the table. I can then iterate over that but I don't know how to update the original DataFrame
.
Essentially what I'm asking is how do I do the following in pandas
:
UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP;
I feel like this should be easy and I'm ashamed that it isn't.
Thank you
python pandas dataframe
Please add an example of input data and expected output.
– user3471881
Nov 21 '18 at 18:15
See: How to make good reproducible pandas examples
– root
Nov 21 '18 at 18:17
look atpd.update()
– Chris
Nov 21 '18 at 18:40
add a comment |
Good morning,
I'm trying to update a DataFrame based on the contents of two columns and am running into issues.
Specifically, I have a column called IP
, another called VISITTIME
. I've added two columns called OLDEST
and NEWEST
which need to contain the min and max VISITTIME
for the IP
of that row.
Using:
df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(),
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")
I can get the min and max times for each IP
in the table. I can then iterate over that but I don't know how to update the original DataFrame
.
Essentially what I'm asking is how do I do the following in pandas
:
UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP;
I feel like this should be easy and I'm ashamed that it isn't.
Thank you
python pandas dataframe
Good morning,
I'm trying to update a DataFrame based on the contents of two columns and am running into issues.
Specifically, I have a column called IP
, another called VISITTIME
. I've added two columns called OLDEST
and NEWEST
which need to contain the min and max VISITTIME
for the IP
of that row.
Using:
df2 = pd.merge(df.groupby('IP')['VISITTIME'].min().to_frame(),
df.groupby('IP')['VISITTIME'].max().to_frame(), on="IP")
I can get the min and max times for each IP
in the table. I can then iterate over that but I don't know how to update the original DataFrame
.
Essentially what I'm asking is how do I do the following in pandas
:
UPDATE df SET df.OLDEST = df2.OLDEST, df.NEWEST = df2.NEWEST WHERE df.IP=df2.IP;
I feel like this should be easy and I'm ashamed that it isn't.
Thank you
python pandas dataframe
python pandas dataframe
edited Nov 21 '18 at 19:43
user3471881
1,1872619
1,1872619
asked Nov 21 '18 at 18:07


DClarkeDClarke
12
12
Please add an example of input data and expected output.
– user3471881
Nov 21 '18 at 18:15
See: How to make good reproducible pandas examples
– root
Nov 21 '18 at 18:17
look atpd.update()
– Chris
Nov 21 '18 at 18:40
add a comment |
Please add an example of input data and expected output.
– user3471881
Nov 21 '18 at 18:15
See: How to make good reproducible pandas examples
– root
Nov 21 '18 at 18:17
look atpd.update()
– Chris
Nov 21 '18 at 18:40
Please add an example of input data and expected output.
– user3471881
Nov 21 '18 at 18:15
Please add an example of input data and expected output.
– user3471881
Nov 21 '18 at 18:15
See: How to make good reproducible pandas examples
– root
Nov 21 '18 at 18:17
See: How to make good reproducible pandas examples
– root
Nov 21 '18 at 18:17
look at
pd.update()
– Chris
Nov 21 '18 at 18:40
look at
pd.update()
– Chris
Nov 21 '18 at 18:40
add a comment |
1 Answer
1
active
oldest
votes
Depending on your version of pandas (I know in version > 0.22.0),
there is a method called Dataframe.update.
That should provide some examples, but a few warnings:
- You need to make sure that both dataframes are using IP as an index,
or this method will not work. - The column (Series) names should be the same, so it will not require any location or column referencing.
- You may choose to only overwrite NA values, but not applicable for what you are describing here.
DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)
Modify in place using non-NA values from another DataFrame.
Aligns on indices. There is no return value.
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%2f53418151%2fupdate-column-a-where-column-b-has-value-c-in-pandas-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Depending on your version of pandas (I know in version > 0.22.0),
there is a method called Dataframe.update.
That should provide some examples, but a few warnings:
- You need to make sure that both dataframes are using IP as an index,
or this method will not work. - The column (Series) names should be the same, so it will not require any location or column referencing.
- You may choose to only overwrite NA values, but not applicable for what you are describing here.
DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)
Modify in place using non-NA values from another DataFrame.
Aligns on indices. There is no return value.
add a comment |
Depending on your version of pandas (I know in version > 0.22.0),
there is a method called Dataframe.update.
That should provide some examples, but a few warnings:
- You need to make sure that both dataframes are using IP as an index,
or this method will not work. - The column (Series) names should be the same, so it will not require any location or column referencing.
- You may choose to only overwrite NA values, but not applicable for what you are describing here.
DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)
Modify in place using non-NA values from another DataFrame.
Aligns on indices. There is no return value.
add a comment |
Depending on your version of pandas (I know in version > 0.22.0),
there is a method called Dataframe.update.
That should provide some examples, but a few warnings:
- You need to make sure that both dataframes are using IP as an index,
or this method will not work. - The column (Series) names should be the same, so it will not require any location or column referencing.
- You may choose to only overwrite NA values, but not applicable for what you are describing here.
DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)
Modify in place using non-NA values from another DataFrame.
Aligns on indices. There is no return value.
Depending on your version of pandas (I know in version > 0.22.0),
there is a method called Dataframe.update.
That should provide some examples, but a few warnings:
- You need to make sure that both dataframes are using IP as an index,
or this method will not work. - The column (Series) names should be the same, so it will not require any location or column referencing.
- You may choose to only overwrite NA values, but not applicable for what you are describing here.
DataFrame.update(other, join='left', overwrite=True, filter_func=None, raise_conflict=False)
Modify in place using non-NA values from another DataFrame.
Aligns on indices. There is no return value.
answered Nov 21 '18 at 18:44


rogersdevoprogersdevop
5613
5613
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%2f53418151%2fupdate-column-a-where-column-b-has-value-c-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
Please add an example of input data and expected output.
– user3471881
Nov 21 '18 at 18:15
See: How to make good reproducible pandas examples
– root
Nov 21 '18 at 18:17
look at
pd.update()
– Chris
Nov 21 '18 at 18:40