Find quick method for Filling Missing Categorical Values with Categories already in the Same Column using a Second Column as CorrespondencePandas fill missing values in dataframe from another dataframeFind maximum value of a column and return the corresponding row values using PandasImpute categorical missing values in scikit-learnReplace substring string in one table with string in another table for an entire columnFinding the corresponding value in pythonReplacing the missing categorical values in a categorical columnPandas - fill missing column values with corresponding id column valuesFilling out missing values in columns?How to fill missing category variable?Pandas Fill column value with next available value in the same columnFilling missing values in dataframe column Python
Unlock your Lock
about to retire but not retired yet, employed but not working any more
What are the occurences of total war in the Native Americans?
Discussing work with supervisor in an invited dinner with his family
Half filled water bottle
How much does Commander Data weigh?
How is linear momentum conserved in case of a freely falling body?
Why did my folder names end up like this, and how can I fix this using a script?
Why is "-ber" the suffix of the last four months of the year?
Is this password scheme legit?
How to say "I only speak one which is English." in French?
What stops you from using fixed income in developing countries?
Why error propagation in CBC mode encryption affect two blocks?
Can I get a PhD for developing an educational software?
Do clerics commit a sin if they do not recite the liturgy of the hours?
How to socialize at pubs/bars when you are alone?
Why is getting a PhD considered "financially irresponsible"?
"There were either twelve sexes or none."
What is the loud noise of a helicopter when the rotors are not yet moving?
What are these white rings in the Undead parish?
Why is strlen so complex in C?
Hangman game in Python - need feedback on the quality of code
Do you pay one or two mana to bounce a transformed Delver of Secrets with Repeal?
What is the meaning of “these lederhosen are riding up my Bundesliga”?
Find quick method for Filling Missing Categorical Values with Categories already in the Same Column using a Second Column as Correspondence
Pandas fill missing values in dataframe from another dataframeFind maximum value of a column and return the corresponding row values using PandasImpute categorical missing values in scikit-learnReplace substring string in one table with string in another table for an entire columnFinding the corresponding value in pythonReplacing the missing categorical values in a categorical columnPandas - fill missing column values with corresponding id column valuesFilling out missing values in columns?How to fill missing category variable?Pandas Fill column value with next available value in the same columnFilling missing values in dataframe column Python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
As you can see in the screenshot, the values of the code column correspond with the values of the name column. That's because the code is for the project code and the name is for the project name.
How can I fill the missing values of the name column using the code column as a correspondence? Is there any method that can take care of that?
code name countryshortname
212 1 Liberia
2 1 Economic management Tunisia
1211 1 Economic management Tonga
1045 1 Economic management Macedonia, former Yugoslav Republic of
363 1 Cote d'Ivoire
453 1 Economic management Lesotho
784 1 Economic management Tanzania
648 1 Economic management Lao People's Democratic Republic
647 1 Economic management Lao People's Democratic Republic
204 1 Economic management Kyrgyz Republic
205 1 Economic management Kyrgyz Republic
249 1 Economic management Armenia
1437 1 Guatemala
1212 1 Economic management Kenya
1114 1 Honduras
[ScreenShot of Actual Dataframe][1]
[1]: https://i.stack.imgur.com/eq6xt.png
python python-3.x
|
show 1 more comment
As you can see in the screenshot, the values of the code column correspond with the values of the name column. That's because the code is for the project code and the name is for the project name.
How can I fill the missing values of the name column using the code column as a correspondence? Is there any method that can take care of that?
code name countryshortname
212 1 Liberia
2 1 Economic management Tunisia
1211 1 Economic management Tonga
1045 1 Economic management Macedonia, former Yugoslav Republic of
363 1 Cote d'Ivoire
453 1 Economic management Lesotho
784 1 Economic management Tanzania
648 1 Economic management Lao People's Democratic Republic
647 1 Economic management Lao People's Democratic Republic
204 1 Economic management Kyrgyz Republic
205 1 Economic management Kyrgyz Republic
249 1 Economic management Armenia
1437 1 Guatemala
1212 1 Economic management Kenya
1114 1 Honduras
[ScreenShot of Actual Dataframe][1]
[1]: https://i.stack.imgur.com/eq6xt.png
python python-3.x
Please provide samples of your dataframes in the question text, not as images. Additionally, what have you tried so far? Please provide a minimal reproducible example
– G. Anderson
Mar 27 at 20:14
i haven't tried anything yet, because the drop methods don't look like they would work. i was trying to write a for loop but i figured i should ask about already existing methods before writing a function. i don't know how to provide a sample without typing out a part of the dataframe.
– Carlos Rivas
Mar 27 at 20:25
It's hard to tell from your question, but this looks like it could be done with a merge or join maybe. As for providing the data, typing up or copy-pasting a sample is what you should do.df.head()
ordf.sample()
are great for creating small dfs to paste in
– G. Anderson
Mar 27 at 20:30
i believe merge is for merging several dataframes on a column, that's not what i'm doing.
– Carlos Rivas
Mar 27 at 20:51
Sorry, I misunderstood the question, but here is a question with some answers you might find helpful
– G. Anderson
Mar 27 at 20:57
|
show 1 more comment
As you can see in the screenshot, the values of the code column correspond with the values of the name column. That's because the code is for the project code and the name is for the project name.
How can I fill the missing values of the name column using the code column as a correspondence? Is there any method that can take care of that?
code name countryshortname
212 1 Liberia
2 1 Economic management Tunisia
1211 1 Economic management Tonga
1045 1 Economic management Macedonia, former Yugoslav Republic of
363 1 Cote d'Ivoire
453 1 Economic management Lesotho
784 1 Economic management Tanzania
648 1 Economic management Lao People's Democratic Republic
647 1 Economic management Lao People's Democratic Republic
204 1 Economic management Kyrgyz Republic
205 1 Economic management Kyrgyz Republic
249 1 Economic management Armenia
1437 1 Guatemala
1212 1 Economic management Kenya
1114 1 Honduras
[ScreenShot of Actual Dataframe][1]
[1]: https://i.stack.imgur.com/eq6xt.png
python python-3.x
As you can see in the screenshot, the values of the code column correspond with the values of the name column. That's because the code is for the project code and the name is for the project name.
How can I fill the missing values of the name column using the code column as a correspondence? Is there any method that can take care of that?
code name countryshortname
212 1 Liberia
2 1 Economic management Tunisia
1211 1 Economic management Tonga
1045 1 Economic management Macedonia, former Yugoslav Republic of
363 1 Cote d'Ivoire
453 1 Economic management Lesotho
784 1 Economic management Tanzania
648 1 Economic management Lao People's Democratic Republic
647 1 Economic management Lao People's Democratic Republic
204 1 Economic management Kyrgyz Republic
205 1 Economic management Kyrgyz Republic
249 1 Economic management Armenia
1437 1 Guatemala
1212 1 Economic management Kenya
1114 1 Honduras
[ScreenShot of Actual Dataframe][1]
[1]: https://i.stack.imgur.com/eq6xt.png
python python-3.x
python python-3.x
edited Mar 27 at 20:37
Carlos Rivas
asked Mar 27 at 19:59
Carlos RivasCarlos Rivas
14 bronze badges
14 bronze badges
Please provide samples of your dataframes in the question text, not as images. Additionally, what have you tried so far? Please provide a minimal reproducible example
– G. Anderson
Mar 27 at 20:14
i haven't tried anything yet, because the drop methods don't look like they would work. i was trying to write a for loop but i figured i should ask about already existing methods before writing a function. i don't know how to provide a sample without typing out a part of the dataframe.
– Carlos Rivas
Mar 27 at 20:25
It's hard to tell from your question, but this looks like it could be done with a merge or join maybe. As for providing the data, typing up or copy-pasting a sample is what you should do.df.head()
ordf.sample()
are great for creating small dfs to paste in
– G. Anderson
Mar 27 at 20:30
i believe merge is for merging several dataframes on a column, that's not what i'm doing.
– Carlos Rivas
Mar 27 at 20:51
Sorry, I misunderstood the question, but here is a question with some answers you might find helpful
– G. Anderson
Mar 27 at 20:57
|
show 1 more comment
Please provide samples of your dataframes in the question text, not as images. Additionally, what have you tried so far? Please provide a minimal reproducible example
– G. Anderson
Mar 27 at 20:14
i haven't tried anything yet, because the drop methods don't look like they would work. i was trying to write a for loop but i figured i should ask about already existing methods before writing a function. i don't know how to provide a sample without typing out a part of the dataframe.
– Carlos Rivas
Mar 27 at 20:25
It's hard to tell from your question, but this looks like it could be done with a merge or join maybe. As for providing the data, typing up or copy-pasting a sample is what you should do.df.head()
ordf.sample()
are great for creating small dfs to paste in
– G. Anderson
Mar 27 at 20:30
i believe merge is for merging several dataframes on a column, that's not what i'm doing.
– Carlos Rivas
Mar 27 at 20:51
Sorry, I misunderstood the question, but here is a question with some answers you might find helpful
– G. Anderson
Mar 27 at 20:57
Please provide samples of your dataframes in the question text, not as images. Additionally, what have you tried so far? Please provide a minimal reproducible example
– G. Anderson
Mar 27 at 20:14
Please provide samples of your dataframes in the question text, not as images. Additionally, what have you tried so far? Please provide a minimal reproducible example
– G. Anderson
Mar 27 at 20:14
i haven't tried anything yet, because the drop methods don't look like they would work. i was trying to write a for loop but i figured i should ask about already existing methods before writing a function. i don't know how to provide a sample without typing out a part of the dataframe.
– Carlos Rivas
Mar 27 at 20:25
i haven't tried anything yet, because the drop methods don't look like they would work. i was trying to write a for loop but i figured i should ask about already existing methods before writing a function. i don't know how to provide a sample without typing out a part of the dataframe.
– Carlos Rivas
Mar 27 at 20:25
It's hard to tell from your question, but this looks like it could be done with a merge or join maybe. As for providing the data, typing up or copy-pasting a sample is what you should do.
df.head()
or df.sample()
are great for creating small dfs to paste in– G. Anderson
Mar 27 at 20:30
It's hard to tell from your question, but this looks like it could be done with a merge or join maybe. As for providing the data, typing up or copy-pasting a sample is what you should do.
df.head()
or df.sample()
are great for creating small dfs to paste in– G. Anderson
Mar 27 at 20:30
i believe merge is for merging several dataframes on a column, that's not what i'm doing.
– Carlos Rivas
Mar 27 at 20:51
i believe merge is for merging several dataframes on a column, that's not what i'm doing.
– Carlos Rivas
Mar 27 at 20:51
Sorry, I misunderstood the question, but here is a question with some answers you might find helpful
– G. Anderson
Mar 27 at 20:57
Sorry, I misunderstood the question, but here is a question with some answers you might find helpful
– G. Anderson
Mar 27 at 20:57
|
show 1 more comment
1 Answer
1
active
oldest
votes
Because there is only one code/name combo in your question, I couldn;t test this more fully. But if you sort by code then name, you should be able to just forward fill the NaN
values
df['name']=df.sort_values(['code','name'])['name'].fillna(method='ffill')
code name countryshortname
212 1 Economic Liberia
2 1 Economic Tunisia
1211 1 Economic Tonga
1045 1 Economic Macedonia
363 1 Economic Cote
453 1 Economic Lesotho
784 1 Economic Tanzania
648 1 Economic Lao
647 1 Economic Lao
204 1 Economic Kyrgyz
205 1 Economic Kyrgyz
249 1 Economic Armenia
1437 1 Economic Guatemala
1212 1 Economic Kenya
1114 1 Economic Honduras
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%2f55385532%2ffind-quick-method-for-filling-missing-categorical-values-with-categories-already%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
Because there is only one code/name combo in your question, I couldn;t test this more fully. But if you sort by code then name, you should be able to just forward fill the NaN
values
df['name']=df.sort_values(['code','name'])['name'].fillna(method='ffill')
code name countryshortname
212 1 Economic Liberia
2 1 Economic Tunisia
1211 1 Economic Tonga
1045 1 Economic Macedonia
363 1 Economic Cote
453 1 Economic Lesotho
784 1 Economic Tanzania
648 1 Economic Lao
647 1 Economic Lao
204 1 Economic Kyrgyz
205 1 Economic Kyrgyz
249 1 Economic Armenia
1437 1 Economic Guatemala
1212 1 Economic Kenya
1114 1 Economic Honduras
add a comment |
Because there is only one code/name combo in your question, I couldn;t test this more fully. But if you sort by code then name, you should be able to just forward fill the NaN
values
df['name']=df.sort_values(['code','name'])['name'].fillna(method='ffill')
code name countryshortname
212 1 Economic Liberia
2 1 Economic Tunisia
1211 1 Economic Tonga
1045 1 Economic Macedonia
363 1 Economic Cote
453 1 Economic Lesotho
784 1 Economic Tanzania
648 1 Economic Lao
647 1 Economic Lao
204 1 Economic Kyrgyz
205 1 Economic Kyrgyz
249 1 Economic Armenia
1437 1 Economic Guatemala
1212 1 Economic Kenya
1114 1 Economic Honduras
add a comment |
Because there is only one code/name combo in your question, I couldn;t test this more fully. But if you sort by code then name, you should be able to just forward fill the NaN
values
df['name']=df.sort_values(['code','name'])['name'].fillna(method='ffill')
code name countryshortname
212 1 Economic Liberia
2 1 Economic Tunisia
1211 1 Economic Tonga
1045 1 Economic Macedonia
363 1 Economic Cote
453 1 Economic Lesotho
784 1 Economic Tanzania
648 1 Economic Lao
647 1 Economic Lao
204 1 Economic Kyrgyz
205 1 Economic Kyrgyz
249 1 Economic Armenia
1437 1 Economic Guatemala
1212 1 Economic Kenya
1114 1 Economic Honduras
Because there is only one code/name combo in your question, I couldn;t test this more fully. But if you sort by code then name, you should be able to just forward fill the NaN
values
df['name']=df.sort_values(['code','name'])['name'].fillna(method='ffill')
code name countryshortname
212 1 Economic Liberia
2 1 Economic Tunisia
1211 1 Economic Tonga
1045 1 Economic Macedonia
363 1 Economic Cote
453 1 Economic Lesotho
784 1 Economic Tanzania
648 1 Economic Lao
647 1 Economic Lao
204 1 Economic Kyrgyz
205 1 Economic Kyrgyz
249 1 Economic Armenia
1437 1 Economic Guatemala
1212 1 Economic Kenya
1114 1 Economic Honduras
answered Mar 28 at 15:45
G. AndersonG. Anderson
2,8411 gold badge6 silver badges13 bronze badges
2,8411 gold badge6 silver badges13 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55385532%2ffind-quick-method-for-filling-missing-categorical-values-with-categories-already%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 provide samples of your dataframes in the question text, not as images. Additionally, what have you tried so far? Please provide a minimal reproducible example
– G. Anderson
Mar 27 at 20:14
i haven't tried anything yet, because the drop methods don't look like they would work. i was trying to write a for loop but i figured i should ask about already existing methods before writing a function. i don't know how to provide a sample without typing out a part of the dataframe.
– Carlos Rivas
Mar 27 at 20:25
It's hard to tell from your question, but this looks like it could be done with a merge or join maybe. As for providing the data, typing up or copy-pasting a sample is what you should do.
df.head()
ordf.sample()
are great for creating small dfs to paste in– G. Anderson
Mar 27 at 20:30
i believe merge is for merging several dataframes on a column, that's not what i'm doing.
– Carlos Rivas
Mar 27 at 20:51
Sorry, I misunderstood the question, but here is a question with some answers you might find helpful
– G. Anderson
Mar 27 at 20:57