How to convert continous data to Categorical in python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
All,
My dataset looks like following, and I Would like to add one column which can convert my last column i.e Day_of_The_week to Mon, Tue, Wed.
Note: Day_of_the_week include 5 days : Mon,Tue,Wed,Thurs,Fri. I checked link but couldn't figure out how to implement in my dataframe
I would like to convert my pandas dataframe as below:
Being a newbie to python,if you could provide explanation, that will be great!
python pandas numeric categorical-data continuous
add a comment |
All,
My dataset looks like following, and I Would like to add one column which can convert my last column i.e Day_of_The_week to Mon, Tue, Wed.
Note: Day_of_the_week include 5 days : Mon,Tue,Wed,Thurs,Fri. I checked link but couldn't figure out how to implement in my dataframe
I would like to convert my pandas dataframe as below:
Being a newbie to python,if you could provide explanation, that will be great!
python pandas numeric categorical-data continuous
add a comment |
All,
My dataset looks like following, and I Would like to add one column which can convert my last column i.e Day_of_The_week to Mon, Tue, Wed.
Note: Day_of_the_week include 5 days : Mon,Tue,Wed,Thurs,Fri. I checked link but couldn't figure out how to implement in my dataframe
I would like to convert my pandas dataframe as below:
Being a newbie to python,if you could provide explanation, that will be great!
python pandas numeric categorical-data continuous
All,
My dataset looks like following, and I Would like to add one column which can convert my last column i.e Day_of_The_week to Mon, Tue, Wed.
Note: Day_of_the_week include 5 days : Mon,Tue,Wed,Thurs,Fri. I checked link but couldn't figure out how to implement in my dataframe
I would like to convert my pandas dataframe as below:
Being a newbie to python,if you could provide explanation, that will be great!
python pandas numeric categorical-data continuous
python pandas numeric categorical-data continuous
asked Nov 26 '18 at 23:50
biggboss2019biggboss2019
9919
9919
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
calendar.day_name
+ pd.Series.map
You can construct a dictionary and then map your integer series to a list of day strings:
import pandas as pd
from calendar import day_name
from collections import deque
df = pd.DataFrame({'Day_of_the_week': [2, 3, 4, 4, 5, 6, 6]})
days = deque(day_name)
days.rotate(df['Day_of_the_week'].iat[0]) # rotate days
days_map = dict(enumerate(days)) # construct dictionary
df['Day_Factor'] = df['Day_of_the_week'].map(days_map)
print(df)
Day_of_the_week Day_Factor
0 2 Monday
1 3 Tuesday
2 4 Wednesday
3 4 Wednesday
4 5 Thursday
5 6 Friday
6 6 Friday
Thanks! The dictionary key's assigned 0 to Friday. I would like to assign key 6 to Friday. Is there a way to do that ? I would like my dictionary to start with 2 as Monday and ends at Key 6- Friday.
– biggboss2019
Nov 27 '18 at 0:13
@biggboss2019, Yes, as you can see that's what my logic does, you are rotating days by 2 days. Since theday_name
has Monday at index 0, but you want it at index 2. To hardcode this, use2
instead ofdf['Day_of_the_week'].iat[0]
.
– jpp
Nov 27 '18 at 0:16
1
Great. Thanks!!
– biggboss2019
Nov 27 '18 at 0:23
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%2f53490801%2fhow-to-convert-continous-data-to-categorical-in-python%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
calendar.day_name
+ pd.Series.map
You can construct a dictionary and then map your integer series to a list of day strings:
import pandas as pd
from calendar import day_name
from collections import deque
df = pd.DataFrame({'Day_of_the_week': [2, 3, 4, 4, 5, 6, 6]})
days = deque(day_name)
days.rotate(df['Day_of_the_week'].iat[0]) # rotate days
days_map = dict(enumerate(days)) # construct dictionary
df['Day_Factor'] = df['Day_of_the_week'].map(days_map)
print(df)
Day_of_the_week Day_Factor
0 2 Monday
1 3 Tuesday
2 4 Wednesday
3 4 Wednesday
4 5 Thursday
5 6 Friday
6 6 Friday
Thanks! The dictionary key's assigned 0 to Friday. I would like to assign key 6 to Friday. Is there a way to do that ? I would like my dictionary to start with 2 as Monday and ends at Key 6- Friday.
– biggboss2019
Nov 27 '18 at 0:13
@biggboss2019, Yes, as you can see that's what my logic does, you are rotating days by 2 days. Since theday_name
has Monday at index 0, but you want it at index 2. To hardcode this, use2
instead ofdf['Day_of_the_week'].iat[0]
.
– jpp
Nov 27 '18 at 0:16
1
Great. Thanks!!
– biggboss2019
Nov 27 '18 at 0:23
add a comment |
calendar.day_name
+ pd.Series.map
You can construct a dictionary and then map your integer series to a list of day strings:
import pandas as pd
from calendar import day_name
from collections import deque
df = pd.DataFrame({'Day_of_the_week': [2, 3, 4, 4, 5, 6, 6]})
days = deque(day_name)
days.rotate(df['Day_of_the_week'].iat[0]) # rotate days
days_map = dict(enumerate(days)) # construct dictionary
df['Day_Factor'] = df['Day_of_the_week'].map(days_map)
print(df)
Day_of_the_week Day_Factor
0 2 Monday
1 3 Tuesday
2 4 Wednesday
3 4 Wednesday
4 5 Thursday
5 6 Friday
6 6 Friday
Thanks! The dictionary key's assigned 0 to Friday. I would like to assign key 6 to Friday. Is there a way to do that ? I would like my dictionary to start with 2 as Monday and ends at Key 6- Friday.
– biggboss2019
Nov 27 '18 at 0:13
@biggboss2019, Yes, as you can see that's what my logic does, you are rotating days by 2 days. Since theday_name
has Monday at index 0, but you want it at index 2. To hardcode this, use2
instead ofdf['Day_of_the_week'].iat[0]
.
– jpp
Nov 27 '18 at 0:16
1
Great. Thanks!!
– biggboss2019
Nov 27 '18 at 0:23
add a comment |
calendar.day_name
+ pd.Series.map
You can construct a dictionary and then map your integer series to a list of day strings:
import pandas as pd
from calendar import day_name
from collections import deque
df = pd.DataFrame({'Day_of_the_week': [2, 3, 4, 4, 5, 6, 6]})
days = deque(day_name)
days.rotate(df['Day_of_the_week'].iat[0]) # rotate days
days_map = dict(enumerate(days)) # construct dictionary
df['Day_Factor'] = df['Day_of_the_week'].map(days_map)
print(df)
Day_of_the_week Day_Factor
0 2 Monday
1 3 Tuesday
2 4 Wednesday
3 4 Wednesday
4 5 Thursday
5 6 Friday
6 6 Friday
calendar.day_name
+ pd.Series.map
You can construct a dictionary and then map your integer series to a list of day strings:
import pandas as pd
from calendar import day_name
from collections import deque
df = pd.DataFrame({'Day_of_the_week': [2, 3, 4, 4, 5, 6, 6]})
days = deque(day_name)
days.rotate(df['Day_of_the_week'].iat[0]) # rotate days
days_map = dict(enumerate(days)) # construct dictionary
df['Day_Factor'] = df['Day_of_the_week'].map(days_map)
print(df)
Day_of_the_week Day_Factor
0 2 Monday
1 3 Tuesday
2 4 Wednesday
3 4 Wednesday
4 5 Thursday
5 6 Friday
6 6 Friday
answered Nov 26 '18 at 23:56
jppjpp
103k2167117
103k2167117
Thanks! The dictionary key's assigned 0 to Friday. I would like to assign key 6 to Friday. Is there a way to do that ? I would like my dictionary to start with 2 as Monday and ends at Key 6- Friday.
– biggboss2019
Nov 27 '18 at 0:13
@biggboss2019, Yes, as you can see that's what my logic does, you are rotating days by 2 days. Since theday_name
has Monday at index 0, but you want it at index 2. To hardcode this, use2
instead ofdf['Day_of_the_week'].iat[0]
.
– jpp
Nov 27 '18 at 0:16
1
Great. Thanks!!
– biggboss2019
Nov 27 '18 at 0:23
add a comment |
Thanks! The dictionary key's assigned 0 to Friday. I would like to assign key 6 to Friday. Is there a way to do that ? I would like my dictionary to start with 2 as Monday and ends at Key 6- Friday.
– biggboss2019
Nov 27 '18 at 0:13
@biggboss2019, Yes, as you can see that's what my logic does, you are rotating days by 2 days. Since theday_name
has Monday at index 0, but you want it at index 2. To hardcode this, use2
instead ofdf['Day_of_the_week'].iat[0]
.
– jpp
Nov 27 '18 at 0:16
1
Great. Thanks!!
– biggboss2019
Nov 27 '18 at 0:23
Thanks! The dictionary key's assigned 0 to Friday. I would like to assign key 6 to Friday. Is there a way to do that ? I would like my dictionary to start with 2 as Monday and ends at Key 6- Friday.
– biggboss2019
Nov 27 '18 at 0:13
Thanks! The dictionary key's assigned 0 to Friday. I would like to assign key 6 to Friday. Is there a way to do that ? I would like my dictionary to start with 2 as Monday and ends at Key 6- Friday.
– biggboss2019
Nov 27 '18 at 0:13
@biggboss2019, Yes, as you can see that's what my logic does, you are rotating days by 2 days. Since the
day_name
has Monday at index 0, but you want it at index 2. To hardcode this, use 2
instead of df['Day_of_the_week'].iat[0]
.– jpp
Nov 27 '18 at 0:16
@biggboss2019, Yes, as you can see that's what my logic does, you are rotating days by 2 days. Since the
day_name
has Monday at index 0, but you want it at index 2. To hardcode this, use 2
instead of df['Day_of_the_week'].iat[0]
.– jpp
Nov 27 '18 at 0:16
1
1
Great. Thanks!!
– biggboss2019
Nov 27 '18 at 0:23
Great. Thanks!!
– biggboss2019
Nov 27 '18 at 0:23
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%2f53490801%2fhow-to-convert-continous-data-to-categorical-in-python%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