Changing 4 columns in only 1
I am trying to convert 4 colunms from my DataFrame to a unique column.
I have the following DataFrame:
doggo floofer pupper puppo
0 None None None None
1 None None None None
2 None None None None
3 None None None None
4 None None None None
5 None None None None
6 None None None None
7 None None None None
8 None None None None
9 doggo None None None
10 None None None None
11 None None None None
12 None None None puppo
13 None None None None
14 None None None puppo
I want a unique column filled with the values 'None', 'doggo', 'floofer', 'pupper', 'puppo'.
I have tried using the Melt function with no success.
my actual code:
melt = pd.melt(melt, id_vars=['doggo', 'floofer', 'pupper', 'puppo'], var_name='classification')
Any help?
EDIT
Below the complete solution(comments are in portuguese):
#criar uma cópia do DataFrame para não comprometer o DataFrame original
df = twitter_archive.copy()
#Apagar os valores None
df = df.replace('None', '')
#criar e preencher a coluna classification com as informações das colunas doggo, floofer, pupper e puppo
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
#Dropar todas as colunas e deixar somente a classification
df = df.drop(columns=['in_reply_to_status_id', 'in_reply_to_user_id', 'timestamp', 'source', 'text', 'retweeted_status_id', 'retweeted_status_user_id', 'retweeted_status_timestamp', 'expanded_urls', 'rating_numerator', 'rating_denominator', 'name', 'doggo','floofer', 'pupper', 'puppo'])
#Acrescentar a coluna classification no DataFrame twitter_archive e remover as colunas doggo, floofer, pupper e puppo
twitter_archive = pd.merge(twitter_archive, df, on= 'tweet_id')
twitter_archive = twitter_archive.drop(columns=['doggo', 'floofer', 'pupper', 'puppo'])
python dataframe multiple-columns melt
add a comment |
I am trying to convert 4 colunms from my DataFrame to a unique column.
I have the following DataFrame:
doggo floofer pupper puppo
0 None None None None
1 None None None None
2 None None None None
3 None None None None
4 None None None None
5 None None None None
6 None None None None
7 None None None None
8 None None None None
9 doggo None None None
10 None None None None
11 None None None None
12 None None None puppo
13 None None None None
14 None None None puppo
I want a unique column filled with the values 'None', 'doggo', 'floofer', 'pupper', 'puppo'.
I have tried using the Melt function with no success.
my actual code:
melt = pd.melt(melt, id_vars=['doggo', 'floofer', 'pupper', 'puppo'], var_name='classification')
Any help?
EDIT
Below the complete solution(comments are in portuguese):
#criar uma cópia do DataFrame para não comprometer o DataFrame original
df = twitter_archive.copy()
#Apagar os valores None
df = df.replace('None', '')
#criar e preencher a coluna classification com as informações das colunas doggo, floofer, pupper e puppo
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
#Dropar todas as colunas e deixar somente a classification
df = df.drop(columns=['in_reply_to_status_id', 'in_reply_to_user_id', 'timestamp', 'source', 'text', 'retweeted_status_id', 'retweeted_status_user_id', 'retweeted_status_timestamp', 'expanded_urls', 'rating_numerator', 'rating_denominator', 'name', 'doggo','floofer', 'pupper', 'puppo'])
#Acrescentar a coluna classification no DataFrame twitter_archive e remover as colunas doggo, floofer, pupper e puppo
twitter_archive = pd.merge(twitter_archive, df, on= 'tweet_id')
twitter_archive = twitter_archive.drop(columns=['doggo', 'floofer', 'pupper', 'puppo'])
python dataframe multiple-columns melt
add a comment |
I am trying to convert 4 colunms from my DataFrame to a unique column.
I have the following DataFrame:
doggo floofer pupper puppo
0 None None None None
1 None None None None
2 None None None None
3 None None None None
4 None None None None
5 None None None None
6 None None None None
7 None None None None
8 None None None None
9 doggo None None None
10 None None None None
11 None None None None
12 None None None puppo
13 None None None None
14 None None None puppo
I want a unique column filled with the values 'None', 'doggo', 'floofer', 'pupper', 'puppo'.
I have tried using the Melt function with no success.
my actual code:
melt = pd.melt(melt, id_vars=['doggo', 'floofer', 'pupper', 'puppo'], var_name='classification')
Any help?
EDIT
Below the complete solution(comments are in portuguese):
#criar uma cópia do DataFrame para não comprometer o DataFrame original
df = twitter_archive.copy()
#Apagar os valores None
df = df.replace('None', '')
#criar e preencher a coluna classification com as informações das colunas doggo, floofer, pupper e puppo
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
#Dropar todas as colunas e deixar somente a classification
df = df.drop(columns=['in_reply_to_status_id', 'in_reply_to_user_id', 'timestamp', 'source', 'text', 'retweeted_status_id', 'retweeted_status_user_id', 'retweeted_status_timestamp', 'expanded_urls', 'rating_numerator', 'rating_denominator', 'name', 'doggo','floofer', 'pupper', 'puppo'])
#Acrescentar a coluna classification no DataFrame twitter_archive e remover as colunas doggo, floofer, pupper e puppo
twitter_archive = pd.merge(twitter_archive, df, on= 'tweet_id')
twitter_archive = twitter_archive.drop(columns=['doggo', 'floofer', 'pupper', 'puppo'])
python dataframe multiple-columns melt
I am trying to convert 4 colunms from my DataFrame to a unique column.
I have the following DataFrame:
doggo floofer pupper puppo
0 None None None None
1 None None None None
2 None None None None
3 None None None None
4 None None None None
5 None None None None
6 None None None None
7 None None None None
8 None None None None
9 doggo None None None
10 None None None None
11 None None None None
12 None None None puppo
13 None None None None
14 None None None puppo
I want a unique column filled with the values 'None', 'doggo', 'floofer', 'pupper', 'puppo'.
I have tried using the Melt function with no success.
my actual code:
melt = pd.melt(melt, id_vars=['doggo', 'floofer', 'pupper', 'puppo'], var_name='classification')
Any help?
EDIT
Below the complete solution(comments are in portuguese):
#criar uma cópia do DataFrame para não comprometer o DataFrame original
df = twitter_archive.copy()
#Apagar os valores None
df = df.replace('None', '')
#criar e preencher a coluna classification com as informações das colunas doggo, floofer, pupper e puppo
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
#Dropar todas as colunas e deixar somente a classification
df = df.drop(columns=['in_reply_to_status_id', 'in_reply_to_user_id', 'timestamp', 'source', 'text', 'retweeted_status_id', 'retweeted_status_user_id', 'retweeted_status_timestamp', 'expanded_urls', 'rating_numerator', 'rating_denominator', 'name', 'doggo','floofer', 'pupper', 'puppo'])
#Acrescentar a coluna classification no DataFrame twitter_archive e remover as colunas doggo, floofer, pupper e puppo
twitter_archive = pd.merge(twitter_archive, df, on= 'tweet_id')
twitter_archive = twitter_archive.drop(columns=['doggo', 'floofer', 'pupper', 'puppo'])
python dataframe multiple-columns melt
python dataframe multiple-columns melt
edited Nov 20 '18 at 14:27
Leandro Baruch
asked Nov 20 '18 at 13:02
Leandro BaruchLeandro Baruch
427
427
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
A quick and dirty way to do it :
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
It helped, but still didnt get there. each line has 4 values now... like NoneNoneNoneNone or doggoNoneNoneNone
– Leandro Baruch
Nov 20 '18 at 13:59
1
I will accept your answer because it helped me to achieve my objective... I will edit my post with the entire solution. Thank you!
– Leandro Baruch
Nov 20 '18 at 14:25
Thank you! My code would work only if you the 'None' you had in your original dataframe werenp.nan
and not strings with "None" value. You can drop.fillna('')
from each column if you don't mind doing it for the entire dataframe (i.e.df = df.replace('None', '')
).
– Siliam
Nov 26 '18 at 10:29
add a comment |
just make take the maximum. Every string is larger than None. Assuming your entries(per line) are unique. The following should work
d = {"col1": [None, "x", None], "col2": ["y",None, None]}
x = pd.DataFrame(d)
x["col3"] = x[["col1", "col2"]].max(axis=1)
Output:
col1 col2 col3
0 None y y
1 x None x
2 None None None
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%2f53393597%2fchanging-4-columns-in-only-1%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
A quick and dirty way to do it :
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
It helped, but still didnt get there. each line has 4 values now... like NoneNoneNoneNone or doggoNoneNoneNone
– Leandro Baruch
Nov 20 '18 at 13:59
1
I will accept your answer because it helped me to achieve my objective... I will edit my post with the entire solution. Thank you!
– Leandro Baruch
Nov 20 '18 at 14:25
Thank you! My code would work only if you the 'None' you had in your original dataframe werenp.nan
and not strings with "None" value. You can drop.fillna('')
from each column if you don't mind doing it for the entire dataframe (i.e.df = df.replace('None', '')
).
– Siliam
Nov 26 '18 at 10:29
add a comment |
A quick and dirty way to do it :
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
It helped, but still didnt get there. each line has 4 values now... like NoneNoneNoneNone or doggoNoneNoneNone
– Leandro Baruch
Nov 20 '18 at 13:59
1
I will accept your answer because it helped me to achieve my objective... I will edit my post with the entire solution. Thank you!
– Leandro Baruch
Nov 20 '18 at 14:25
Thank you! My code would work only if you the 'None' you had in your original dataframe werenp.nan
and not strings with "None" value. You can drop.fillna('')
from each column if you don't mind doing it for the entire dataframe (i.e.df = df.replace('None', '')
).
– Siliam
Nov 26 '18 at 10:29
add a comment |
A quick and dirty way to do it :
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
A quick and dirty way to do it :
df['classification'] = (df['doggo'].fillna('') + df['floofer'].fillna('') + df['pupper'].fillna('') + df['puppo'].fillna('')).replace('', np.nan)
answered Nov 20 '18 at 13:35
SiliamSiliam
563
563
It helped, but still didnt get there. each line has 4 values now... like NoneNoneNoneNone or doggoNoneNoneNone
– Leandro Baruch
Nov 20 '18 at 13:59
1
I will accept your answer because it helped me to achieve my objective... I will edit my post with the entire solution. Thank you!
– Leandro Baruch
Nov 20 '18 at 14:25
Thank you! My code would work only if you the 'None' you had in your original dataframe werenp.nan
and not strings with "None" value. You can drop.fillna('')
from each column if you don't mind doing it for the entire dataframe (i.e.df = df.replace('None', '')
).
– Siliam
Nov 26 '18 at 10:29
add a comment |
It helped, but still didnt get there. each line has 4 values now... like NoneNoneNoneNone or doggoNoneNoneNone
– Leandro Baruch
Nov 20 '18 at 13:59
1
I will accept your answer because it helped me to achieve my objective... I will edit my post with the entire solution. Thank you!
– Leandro Baruch
Nov 20 '18 at 14:25
Thank you! My code would work only if you the 'None' you had in your original dataframe werenp.nan
and not strings with "None" value. You can drop.fillna('')
from each column if you don't mind doing it for the entire dataframe (i.e.df = df.replace('None', '')
).
– Siliam
Nov 26 '18 at 10:29
It helped, but still didnt get there. each line has 4 values now... like NoneNoneNoneNone or doggoNoneNoneNone
– Leandro Baruch
Nov 20 '18 at 13:59
It helped, but still didnt get there. each line has 4 values now... like NoneNoneNoneNone or doggoNoneNoneNone
– Leandro Baruch
Nov 20 '18 at 13:59
1
1
I will accept your answer because it helped me to achieve my objective... I will edit my post with the entire solution. Thank you!
– Leandro Baruch
Nov 20 '18 at 14:25
I will accept your answer because it helped me to achieve my objective... I will edit my post with the entire solution. Thank you!
– Leandro Baruch
Nov 20 '18 at 14:25
Thank you! My code would work only if you the 'None' you had in your original dataframe were
np.nan
and not strings with "None" value. You can drop .fillna('')
from each column if you don't mind doing it for the entire dataframe (i.e. df = df.replace('None', '')
).– Siliam
Nov 26 '18 at 10:29
Thank you! My code would work only if you the 'None' you had in your original dataframe were
np.nan
and not strings with "None" value. You can drop .fillna('')
from each column if you don't mind doing it for the entire dataframe (i.e. df = df.replace('None', '')
).– Siliam
Nov 26 '18 at 10:29
add a comment |
just make take the maximum. Every string is larger than None. Assuming your entries(per line) are unique. The following should work
d = {"col1": [None, "x", None], "col2": ["y",None, None]}
x = pd.DataFrame(d)
x["col3"] = x[["col1", "col2"]].max(axis=1)
Output:
col1 col2 col3
0 None y y
1 x None x
2 None None None
add a comment |
just make take the maximum. Every string is larger than None. Assuming your entries(per line) are unique. The following should work
d = {"col1": [None, "x", None], "col2": ["y",None, None]}
x = pd.DataFrame(d)
x["col3"] = x[["col1", "col2"]].max(axis=1)
Output:
col1 col2 col3
0 None y y
1 x None x
2 None None None
add a comment |
just make take the maximum. Every string is larger than None. Assuming your entries(per line) are unique. The following should work
d = {"col1": [None, "x", None], "col2": ["y",None, None]}
x = pd.DataFrame(d)
x["col3"] = x[["col1", "col2"]].max(axis=1)
Output:
col1 col2 col3
0 None y y
1 x None x
2 None None None
just make take the maximum. Every string is larger than None. Assuming your entries(per line) are unique. The following should work
d = {"col1": [None, "x", None], "col2": ["y",None, None]}
x = pd.DataFrame(d)
x["col3"] = x[["col1", "col2"]].max(axis=1)
Output:
col1 col2 col3
0 None y y
1 x None x
2 None None None
answered Nov 20 '18 at 13:25
GlostasGlostas
502416
502416
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%2f53393597%2fchanging-4-columns-in-only-1%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