Python-Replace rows in a column using condition in other column using dictionary
A B
abc AN
abd BN
a01 CN
abc BN
a02 CN
I have a dataframe similar to above. For every B=CN
,I want to replace corresponding row in A i.e a01
,a02
with value from dictionary.
The dictionary is as follow:
d={'a01':'ana','a02':'aza'}
Whenever I'm trying to replace the values, it's giving incorrect result either the values are misplaced. I tried where clause as well but still missing something.
python-3.x pandas dataframe data-science
add a comment |
A B
abc AN
abd BN
a01 CN
abc BN
a02 CN
I have a dataframe similar to above. For every B=CN
,I want to replace corresponding row in A i.e a01
,a02
with value from dictionary.
The dictionary is as follow:
d={'a01':'ana','a02':'aza'}
Whenever I'm trying to replace the values, it's giving incorrect result either the values are misplaced. I tried where clause as well but still missing something.
python-3.x pandas dataframe data-science
1
Can you rephrase the question, specifically can you give more details about the "dataframe": is it a text file, array or some sort of data-structure? Maybe provide some existing code, MWE.
– Emil Vatai
Nov 15 '18 at 5:28
@EmilVatai There is a csv file with above dataframe.A and B are column of object type.I've to replace few values in rows of column A i.e a01 and a02 with values in dictionary d but the condition is only if B is equal to CN.
– VroJ
Nov 15 '18 at 6:00
pandas.DataFrame? That part of your question is very vague... You want to change the csv or the datastructure? Could you provide a MVE?
– Emil Vatai
Nov 15 '18 at 6:22
X=pd.read_csv('somefile.csv). A and B are columns in X.I've to replace few rows in A which have values a01 and a02 but only if the values in column B is BN.I've created a dictionary d whose value are to be used for replacing rows in A.For example: a01 should be replaced by ana only if rows in B have value equal to CN .Is it clear now?
– VroJ
Nov 15 '18 at 7:18
add a comment |
A B
abc AN
abd BN
a01 CN
abc BN
a02 CN
I have a dataframe similar to above. For every B=CN
,I want to replace corresponding row in A i.e a01
,a02
with value from dictionary.
The dictionary is as follow:
d={'a01':'ana','a02':'aza'}
Whenever I'm trying to replace the values, it's giving incorrect result either the values are misplaced. I tried where clause as well but still missing something.
python-3.x pandas dataframe data-science
A B
abc AN
abd BN
a01 CN
abc BN
a02 CN
I have a dataframe similar to above. For every B=CN
,I want to replace corresponding row in A i.e a01
,a02
with value from dictionary.
The dictionary is as follow:
d={'a01':'ana','a02':'aza'}
Whenever I'm trying to replace the values, it's giving incorrect result either the values are misplaced. I tried where clause as well but still missing something.
python-3.x pandas dataframe data-science
python-3.x pandas dataframe data-science
edited Nov 15 '18 at 9:52
Akhilesh Pandey
549313
549313
asked Nov 15 '18 at 5:23
VroJVroJ
255
255
1
Can you rephrase the question, specifically can you give more details about the "dataframe": is it a text file, array or some sort of data-structure? Maybe provide some existing code, MWE.
– Emil Vatai
Nov 15 '18 at 5:28
@EmilVatai There is a csv file with above dataframe.A and B are column of object type.I've to replace few values in rows of column A i.e a01 and a02 with values in dictionary d but the condition is only if B is equal to CN.
– VroJ
Nov 15 '18 at 6:00
pandas.DataFrame? That part of your question is very vague... You want to change the csv or the datastructure? Could you provide a MVE?
– Emil Vatai
Nov 15 '18 at 6:22
X=pd.read_csv('somefile.csv). A and B are columns in X.I've to replace few rows in A which have values a01 and a02 but only if the values in column B is BN.I've created a dictionary d whose value are to be used for replacing rows in A.For example: a01 should be replaced by ana only if rows in B have value equal to CN .Is it clear now?
– VroJ
Nov 15 '18 at 7:18
add a comment |
1
Can you rephrase the question, specifically can you give more details about the "dataframe": is it a text file, array or some sort of data-structure? Maybe provide some existing code, MWE.
– Emil Vatai
Nov 15 '18 at 5:28
@EmilVatai There is a csv file with above dataframe.A and B are column of object type.I've to replace few values in rows of column A i.e a01 and a02 with values in dictionary d but the condition is only if B is equal to CN.
– VroJ
Nov 15 '18 at 6:00
pandas.DataFrame? That part of your question is very vague... You want to change the csv or the datastructure? Could you provide a MVE?
– Emil Vatai
Nov 15 '18 at 6:22
X=pd.read_csv('somefile.csv). A and B are columns in X.I've to replace few rows in A which have values a01 and a02 but only if the values in column B is BN.I've created a dictionary d whose value are to be used for replacing rows in A.For example: a01 should be replaced by ana only if rows in B have value equal to CN .Is it clear now?
– VroJ
Nov 15 '18 at 7:18
1
1
Can you rephrase the question, specifically can you give more details about the "dataframe": is it a text file, array or some sort of data-structure? Maybe provide some existing code, MWE.
– Emil Vatai
Nov 15 '18 at 5:28
Can you rephrase the question, specifically can you give more details about the "dataframe": is it a text file, array or some sort of data-structure? Maybe provide some existing code, MWE.
– Emil Vatai
Nov 15 '18 at 5:28
@EmilVatai There is a csv file with above dataframe.A and B are column of object type.I've to replace few values in rows of column A i.e a01 and a02 with values in dictionary d but the condition is only if B is equal to CN.
– VroJ
Nov 15 '18 at 6:00
@EmilVatai There is a csv file with above dataframe.A and B are column of object type.I've to replace few values in rows of column A i.e a01 and a02 with values in dictionary d but the condition is only if B is equal to CN.
– VroJ
Nov 15 '18 at 6:00
pandas.DataFrame? That part of your question is very vague... You want to change the csv or the datastructure? Could you provide a MVE?
– Emil Vatai
Nov 15 '18 at 6:22
pandas.DataFrame? That part of your question is very vague... You want to change the csv or the datastructure? Could you provide a MVE?
– Emil Vatai
Nov 15 '18 at 6:22
X=pd.read_csv('somefile.csv). A and B are columns in X.I've to replace few rows in A which have values a01 and a02 but only if the values in column B is BN.I've created a dictionary d whose value are to be used for replacing rows in A.For example: a01 should be replaced by ana only if rows in B have value equal to CN .Is it clear now?
– VroJ
Nov 15 '18 at 7:18
X=pd.read_csv('somefile.csv). A and B are columns in X.I've to replace few rows in A which have values a01 and a02 but only if the values in column B is BN.I've created a dictionary d whose value are to be used for replacing rows in A.For example: a01 should be replaced by ana only if rows in B have value equal to CN .Is it clear now?
– VroJ
Nov 15 '18 at 7:18
add a comment |
2 Answers
2
active
oldest
votes
In a single line in would look like this:
df.loc[df['B'].eq('CN'), 'A'] = df['A'].map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
Also you can use np.where:
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
Here is the whole code with np.where
:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['abc', 'abd', 'a01', 'abc', 'a02'],
'B': ['AN', 'BN', 'CN', 'BN', 'CN']})
d = {'a01': 'ana', 'a02': 'aza'}
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
0n applying it,I get 'nan' for replaced values in A.
– VroJ
Nov 15 '18 at 14:22
@VroJ I've added code for your example and it works as intended.
– zipa
Nov 15 '18 at 14:27
thank you very much.
– VroJ
Nov 16 '18 at 22:42
add a comment |
Use map
to use the dictionary for mapping the values and loc
to assign the values to the particular slice:
CN = df['B'] == 'CN'
df['A'].loc[CN] = df[CN].A.map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
0n applying it,I get 'nan' for replaced values in A.There is a warning "A value is trying to be set on a copy of a slice from a DataFrame".
– VroJ
Nov 15 '18 at 14:18
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%2f53312933%2fpython-replace-rows-in-a-column-using-condition-in-other-column-using-dictionary%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
In a single line in would look like this:
df.loc[df['B'].eq('CN'), 'A'] = df['A'].map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
Also you can use np.where:
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
Here is the whole code with np.where
:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['abc', 'abd', 'a01', 'abc', 'a02'],
'B': ['AN', 'BN', 'CN', 'BN', 'CN']})
d = {'a01': 'ana', 'a02': 'aza'}
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
0n applying it,I get 'nan' for replaced values in A.
– VroJ
Nov 15 '18 at 14:22
@VroJ I've added code for your example and it works as intended.
– zipa
Nov 15 '18 at 14:27
thank you very much.
– VroJ
Nov 16 '18 at 22:42
add a comment |
In a single line in would look like this:
df.loc[df['B'].eq('CN'), 'A'] = df['A'].map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
Also you can use np.where:
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
Here is the whole code with np.where
:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['abc', 'abd', 'a01', 'abc', 'a02'],
'B': ['AN', 'BN', 'CN', 'BN', 'CN']})
d = {'a01': 'ana', 'a02': 'aza'}
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
0n applying it,I get 'nan' for replaced values in A.
– VroJ
Nov 15 '18 at 14:22
@VroJ I've added code for your example and it works as intended.
– zipa
Nov 15 '18 at 14:27
thank you very much.
– VroJ
Nov 16 '18 at 22:42
add a comment |
In a single line in would look like this:
df.loc[df['B'].eq('CN'), 'A'] = df['A'].map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
Also you can use np.where:
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
Here is the whole code with np.where
:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['abc', 'abd', 'a01', 'abc', 'a02'],
'B': ['AN', 'BN', 'CN', 'BN', 'CN']})
d = {'a01': 'ana', 'a02': 'aza'}
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
In a single line in would look like this:
df.loc[df['B'].eq('CN'), 'A'] = df['A'].map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
Also you can use np.where:
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
Here is the whole code with np.where
:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['abc', 'abd', 'a01', 'abc', 'a02'],
'B': ['AN', 'BN', 'CN', 'BN', 'CN']})
d = {'a01': 'ana', 'a02': 'aza'}
df['A'] = np.where(df['B'].eq('CN'), df['A'].map(d), df['A'])
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
edited Nov 15 '18 at 14:27
answered Nov 15 '18 at 10:15
zipazipa
16.2k31637
16.2k31637
0n applying it,I get 'nan' for replaced values in A.
– VroJ
Nov 15 '18 at 14:22
@VroJ I've added code for your example and it works as intended.
– zipa
Nov 15 '18 at 14:27
thank you very much.
– VroJ
Nov 16 '18 at 22:42
add a comment |
0n applying it,I get 'nan' for replaced values in A.
– VroJ
Nov 15 '18 at 14:22
@VroJ I've added code for your example and it works as intended.
– zipa
Nov 15 '18 at 14:27
thank you very much.
– VroJ
Nov 16 '18 at 22:42
0n applying it,I get 'nan' for replaced values in A.
– VroJ
Nov 15 '18 at 14:22
0n applying it,I get 'nan' for replaced values in A.
– VroJ
Nov 15 '18 at 14:22
@VroJ I've added code for your example and it works as intended.
– zipa
Nov 15 '18 at 14:27
@VroJ I've added code for your example and it works as intended.
– zipa
Nov 15 '18 at 14:27
thank you very much.
– VroJ
Nov 16 '18 at 22:42
thank you very much.
– VroJ
Nov 16 '18 at 22:42
add a comment |
Use map
to use the dictionary for mapping the values and loc
to assign the values to the particular slice:
CN = df['B'] == 'CN'
df['A'].loc[CN] = df[CN].A.map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
0n applying it,I get 'nan' for replaced values in A.There is a warning "A value is trying to be set on a copy of a slice from a DataFrame".
– VroJ
Nov 15 '18 at 14:18
add a comment |
Use map
to use the dictionary for mapping the values and loc
to assign the values to the particular slice:
CN = df['B'] == 'CN'
df['A'].loc[CN] = df[CN].A.map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
0n applying it,I get 'nan' for replaced values in A.There is a warning "A value is trying to be set on a copy of a slice from a DataFrame".
– VroJ
Nov 15 '18 at 14:18
add a comment |
Use map
to use the dictionary for mapping the values and loc
to assign the values to the particular slice:
CN = df['B'] == 'CN'
df['A'].loc[CN] = df[CN].A.map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
Use map
to use the dictionary for mapping the values and loc
to assign the values to the particular slice:
CN = df['B'] == 'CN'
df['A'].loc[CN] = df[CN].A.map(d)
df
A B
0 abc AN
1 abd BN
2 ana CN
3 abc BN
4 aza CN
answered Nov 15 '18 at 7:23
Franco PiccoloFranco Piccolo
1,601714
1,601714
0n applying it,I get 'nan' for replaced values in A.There is a warning "A value is trying to be set on a copy of a slice from a DataFrame".
– VroJ
Nov 15 '18 at 14:18
add a comment |
0n applying it,I get 'nan' for replaced values in A.There is a warning "A value is trying to be set on a copy of a slice from a DataFrame".
– VroJ
Nov 15 '18 at 14:18
0n applying it,I get 'nan' for replaced values in A.There is a warning "A value is trying to be set on a copy of a slice from a DataFrame".
– VroJ
Nov 15 '18 at 14:18
0n applying it,I get 'nan' for replaced values in A.There is a warning "A value is trying to be set on a copy of a slice from a DataFrame".
– VroJ
Nov 15 '18 at 14:18
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%2f53312933%2fpython-replace-rows-in-a-column-using-condition-in-other-column-using-dictionary%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
Can you rephrase the question, specifically can you give more details about the "dataframe": is it a text file, array or some sort of data-structure? Maybe provide some existing code, MWE.
– Emil Vatai
Nov 15 '18 at 5:28
@EmilVatai There is a csv file with above dataframe.A and B are column of object type.I've to replace few values in rows of column A i.e a01 and a02 with values in dictionary d but the condition is only if B is equal to CN.
– VroJ
Nov 15 '18 at 6:00
pandas.DataFrame? That part of your question is very vague... You want to change the csv or the datastructure? Could you provide a MVE?
– Emil Vatai
Nov 15 '18 at 6:22
X=pd.read_csv('somefile.csv). A and B are columns in X.I've to replace few rows in A which have values a01 and a02 but only if the values in column B is BN.I've created a dictionary d whose value are to be used for replacing rows in A.For example: a01 should be replaced by ana only if rows in B have value equal to CN .Is it clear now?
– VroJ
Nov 15 '18 at 7:18