Data Formatting pandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to enter a line of code that creates a row for the index 31'st January 1995
. I am unable to get the row to look like 31/01/1995
and instead the output is 1995-01-31 00:00:00
.
My original data in a dataframe called MainData
I am trying to add a row at the top for 31st January 1995 in the same format as the data below.
My code is
MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData
Please let me know if there is a way to reformat this to 31/01/1995
.
Thanks in advance.
python pandas datetime
|
show 2 more comments
I am trying to enter a line of code that creates a row for the index 31'st January 1995
. I am unable to get the row to look like 31/01/1995
and instead the output is 1995-01-31 00:00:00
.
My original data in a dataframe called MainData
I am trying to add a row at the top for 31st January 1995 in the same format as the data below.
My code is
MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData
Please let me know if there is a way to reformat this to 31/01/1995
.
Thanks in advance.
python pandas datetime
Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?
– AnalysisNerd
Nov 22 '18 at 20:33
I have no idea. Please make an MCVE
– roganjosh
Nov 22 '18 at 20:34
MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.
– AnalysisNerd
Nov 22 '18 at 20:34
I think it's better write a function that convertdatetime
type intostring
and then apply it on your column.
– Ali AzG
Nov 22 '18 at 20:35
1
Am editing my query to provide further information
– AnalysisNerd
Nov 22 '18 at 20:36
|
show 2 more comments
I am trying to enter a line of code that creates a row for the index 31'st January 1995
. I am unable to get the row to look like 31/01/1995
and instead the output is 1995-01-31 00:00:00
.
My original data in a dataframe called MainData
I am trying to add a row at the top for 31st January 1995 in the same format as the data below.
My code is
MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData
Please let me know if there is a way to reformat this to 31/01/1995
.
Thanks in advance.
python pandas datetime
I am trying to enter a line of code that creates a row for the index 31'st January 1995
. I am unable to get the row to look like 31/01/1995
and instead the output is 1995-01-31 00:00:00
.
My original data in a dataframe called MainData
I am trying to add a row at the top for 31st January 1995 in the same format as the data below.
My code is
MainData.loc[pd.to_datetime('31/01/1995',format='%d/%m/%Y'),:] = [100 for number in range(7)]
MainData
Please let me know if there is a way to reformat this to 31/01/1995
.
Thanks in advance.
python pandas datetime
python pandas datetime
edited Nov 22 '18 at 20:39
AnalysisNerd
asked Nov 22 '18 at 20:28
AnalysisNerdAnalysisNerd
69111
69111
Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?
– AnalysisNerd
Nov 22 '18 at 20:33
I have no idea. Please make an MCVE
– roganjosh
Nov 22 '18 at 20:34
MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.
– AnalysisNerd
Nov 22 '18 at 20:34
I think it's better write a function that convertdatetime
type intostring
and then apply it on your column.
– Ali AzG
Nov 22 '18 at 20:35
1
Am editing my query to provide further information
– AnalysisNerd
Nov 22 '18 at 20:36
|
show 2 more comments
Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?
– AnalysisNerd
Nov 22 '18 at 20:33
I have no idea. Please make an MCVE
– roganjosh
Nov 22 '18 at 20:34
MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.
– AnalysisNerd
Nov 22 '18 at 20:34
I think it's better write a function that convertdatetime
type intostring
and then apply it on your column.
– Ali AzG
Nov 22 '18 at 20:35
1
Am editing my query to provide further information
– AnalysisNerd
Nov 22 '18 at 20:36
Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?
– AnalysisNerd
Nov 22 '18 at 20:33
Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?
– AnalysisNerd
Nov 22 '18 at 20:33
I have no idea. Please make an MCVE
– roganjosh
Nov 22 '18 at 20:34
I have no idea. Please make an MCVE
– roganjosh
Nov 22 '18 at 20:34
MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.
– AnalysisNerd
Nov 22 '18 at 20:34
MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.
– AnalysisNerd
Nov 22 '18 at 20:34
I think it's better write a function that convert
datetime
type into string
and then apply it on your column.– Ali AzG
Nov 22 '18 at 20:35
I think it's better write a function that convert
datetime
type into string
and then apply it on your column.– Ali AzG
Nov 22 '18 at 20:35
1
1
Am editing my query to provide further information
– AnalysisNerd
Nov 22 '18 at 20:36
Am editing my query to provide further information
– AnalysisNerd
Nov 22 '18 at 20:36
|
show 2 more comments
1 Answer
1
active
oldest
votes
#Making the data look more normal by removing the first column index level
MainData = MainData.rename(columns=MainData.iloc[0])
MainData = MainData.iloc[1:]
#Re-adjusting the Index to a datetime format
MainData['DateAdjusted'] = MainData.index
MainData = MainData.reset_index(drop=True)
MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)
#Just renaming the Column and converting the index back to Date
MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
MainData.index = MainData['Date']
del MainData['Date']
#Defining the date for the row I want to add
InitialDate = "31/01/1995"
format_str = '%d/%m/%Y'
datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
print (datetime_obj.date())
MainData.loc[datetime_obj,:] = [100 for number in range(7)]
MainData = MainData.sort_index(ascending=True)
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%2f53437655%2fdata-formatting-pandas%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
#Making the data look more normal by removing the first column index level
MainData = MainData.rename(columns=MainData.iloc[0])
MainData = MainData.iloc[1:]
#Re-adjusting the Index to a datetime format
MainData['DateAdjusted'] = MainData.index
MainData = MainData.reset_index(drop=True)
MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)
#Just renaming the Column and converting the index back to Date
MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
MainData.index = MainData['Date']
del MainData['Date']
#Defining the date for the row I want to add
InitialDate = "31/01/1995"
format_str = '%d/%m/%Y'
datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
print (datetime_obj.date())
MainData.loc[datetime_obj,:] = [100 for number in range(7)]
MainData = MainData.sort_index(ascending=True)
add a comment |
#Making the data look more normal by removing the first column index level
MainData = MainData.rename(columns=MainData.iloc[0])
MainData = MainData.iloc[1:]
#Re-adjusting the Index to a datetime format
MainData['DateAdjusted'] = MainData.index
MainData = MainData.reset_index(drop=True)
MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)
#Just renaming the Column and converting the index back to Date
MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
MainData.index = MainData['Date']
del MainData['Date']
#Defining the date for the row I want to add
InitialDate = "31/01/1995"
format_str = '%d/%m/%Y'
datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
print (datetime_obj.date())
MainData.loc[datetime_obj,:] = [100 for number in range(7)]
MainData = MainData.sort_index(ascending=True)
add a comment |
#Making the data look more normal by removing the first column index level
MainData = MainData.rename(columns=MainData.iloc[0])
MainData = MainData.iloc[1:]
#Re-adjusting the Index to a datetime format
MainData['DateAdjusted'] = MainData.index
MainData = MainData.reset_index(drop=True)
MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)
#Just renaming the Column and converting the index back to Date
MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
MainData.index = MainData['Date']
del MainData['Date']
#Defining the date for the row I want to add
InitialDate = "31/01/1995"
format_str = '%d/%m/%Y'
datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
print (datetime_obj.date())
MainData.loc[datetime_obj,:] = [100 for number in range(7)]
MainData = MainData.sort_index(ascending=True)
#Making the data look more normal by removing the first column index level
MainData = MainData.rename(columns=MainData.iloc[0])
MainData = MainData.iloc[1:]
#Re-adjusting the Index to a datetime format
MainData['DateAdjusted'] = MainData.index
MainData = MainData.reset_index(drop=True)
MainData['DateAdjusted'] = pd.to_datetime(MainData['DateAdjusted'],dayfirst=True)
#Just renaming the Column and converting the index back to Date
MainData.rename(columns={'DateAdjusted':'Date'},inplace=True)
MainData.index = MainData['Date']
del MainData['Date']
#Defining the date for the row I want to add
InitialDate = "31/01/1995"
format_str = '%d/%m/%Y'
datetime_obj = datetime.datetime.strptime(InitialDate, format_str)
print (datetime_obj.date())
MainData.loc[datetime_obj,:] = [100 for number in range(7)]
MainData = MainData.sort_index(ascending=True)
answered Nov 22 '18 at 21:50
AnalysisNerdAnalysisNerd
69111
69111
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%2f53437655%2fdata-formatting-pandas%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
Yes, I need the index to look like "31/01/1995" and not the way it is in the output. How do I do that?
– AnalysisNerd
Nov 22 '18 at 20:33
I have no idea. Please make an MCVE
– roganjosh
Nov 22 '18 at 20:34
MainData is the main dataframe where I am trying to add a row with "31/01/1995". There is no df.
– AnalysisNerd
Nov 22 '18 at 20:34
I think it's better write a function that convert
datetime
type intostring
and then apply it on your column.– Ali AzG
Nov 22 '18 at 20:35
1
Am editing my query to provide further information
– AnalysisNerd
Nov 22 '18 at 20:36