Replace zeros with last value different from zero
I have the following dataframe:
print(inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 0 0
3 12/09/18 2 0 0
4 13/09/18 0 0 0
5 14/09/18 4 30 1
I would like to change the values equal to zero, with the last value != from zero, in each columns, as:
print(final_inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
How could I do it?
python pandas
add a comment |
I have the following dataframe:
print(inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 0 0
3 12/09/18 2 0 0
4 13/09/18 0 0 0
5 14/09/18 4 30 1
I would like to change the values equal to zero, with the last value != from zero, in each columns, as:
print(final_inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
How could I do it?
python pandas
add a comment |
I have the following dataframe:
print(inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 0 0
3 12/09/18 2 0 0
4 13/09/18 0 0 0
5 14/09/18 4 30 1
I would like to change the values equal to zero, with the last value != from zero, in each columns, as:
print(final_inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
How could I do it?
python pandas
I have the following dataframe:
print(inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 0 0
3 12/09/18 2 0 0
4 13/09/18 0 0 0
5 14/09/18 4 30 1
I would like to change the values equal to zero, with the last value != from zero, in each columns, as:
print(final_inventory_df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
How could I do it?
python pandas
python pandas
asked Nov 23 '18 at 9:46
Alessandro CeccarelliAlessandro Ceccarelli
252211
252211
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Idea is replace 0 to NaNs by mask and then forward filling them by previous non missing values:
cols = df.columns.difference(['dt_op'])
df[cols] = df[cols].mask(df[cols] == 0).ffill().astype(int)
Similar solution with numpy.where:
df[cols] = pd.DataFrame(np.where(df[cols] == 0, np.nan, df[cols]),
index=df.index,
columns=cols).ffill().astype(int)
print (df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
Solution for fun - convert to integer all columns without dt_op:
d = dict.fromkeys(df.columns.difference(['dt_op']), 'int')
df = df.mask(df == 0).ffill().astype(d)
1
Another excellent piece +1
– pygo
Nov 23 '18 at 10:45
What if it reports: ValueError: Cannot convert non-finite values (NA or inf) to integer? @jezrael
– Alessandro Ceccarelli
Nov 23 '18 at 14:16
@AlessandroCeccarelli - It means there is someNaNvalue, so only remove.astype(int)
– jezrael
Nov 23 '18 at 14:17
@AlessandroCeccarelli - BecauseNaNisfloattype and cannot be casted to integers, so raise error
– jezrael
Nov 23 '18 at 14:18
1
Now it is working properly! Many thanks
– Alessandro Ceccarelli
Nov 23 '18 at 14:35
|
show 6 more comments
Just another option:
df.iloc[:,1:] = df.iloc[:,1:].replace(0, np.nan).ffill().astype(int)
1
This is an excellent show with slicing +1
– pygo
Nov 23 '18 at 10:46
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%2f53444173%2freplace-zeros-with-last-value-different-from-zero%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
Idea is replace 0 to NaNs by mask and then forward filling them by previous non missing values:
cols = df.columns.difference(['dt_op'])
df[cols] = df[cols].mask(df[cols] == 0).ffill().astype(int)
Similar solution with numpy.where:
df[cols] = pd.DataFrame(np.where(df[cols] == 0, np.nan, df[cols]),
index=df.index,
columns=cols).ffill().astype(int)
print (df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
Solution for fun - convert to integer all columns without dt_op:
d = dict.fromkeys(df.columns.difference(['dt_op']), 'int')
df = df.mask(df == 0).ffill().astype(d)
1
Another excellent piece +1
– pygo
Nov 23 '18 at 10:45
What if it reports: ValueError: Cannot convert non-finite values (NA or inf) to integer? @jezrael
– Alessandro Ceccarelli
Nov 23 '18 at 14:16
@AlessandroCeccarelli - It means there is someNaNvalue, so only remove.astype(int)
– jezrael
Nov 23 '18 at 14:17
@AlessandroCeccarelli - BecauseNaNisfloattype and cannot be casted to integers, so raise error
– jezrael
Nov 23 '18 at 14:18
1
Now it is working properly! Many thanks
– Alessandro Ceccarelli
Nov 23 '18 at 14:35
|
show 6 more comments
Idea is replace 0 to NaNs by mask and then forward filling them by previous non missing values:
cols = df.columns.difference(['dt_op'])
df[cols] = df[cols].mask(df[cols] == 0).ffill().astype(int)
Similar solution with numpy.where:
df[cols] = pd.DataFrame(np.where(df[cols] == 0, np.nan, df[cols]),
index=df.index,
columns=cols).ffill().astype(int)
print (df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
Solution for fun - convert to integer all columns without dt_op:
d = dict.fromkeys(df.columns.difference(['dt_op']), 'int')
df = df.mask(df == 0).ffill().astype(d)
1
Another excellent piece +1
– pygo
Nov 23 '18 at 10:45
What if it reports: ValueError: Cannot convert non-finite values (NA or inf) to integer? @jezrael
– Alessandro Ceccarelli
Nov 23 '18 at 14:16
@AlessandroCeccarelli - It means there is someNaNvalue, so only remove.astype(int)
– jezrael
Nov 23 '18 at 14:17
@AlessandroCeccarelli - BecauseNaNisfloattype and cannot be casted to integers, so raise error
– jezrael
Nov 23 '18 at 14:18
1
Now it is working properly! Many thanks
– Alessandro Ceccarelli
Nov 23 '18 at 14:35
|
show 6 more comments
Idea is replace 0 to NaNs by mask and then forward filling them by previous non missing values:
cols = df.columns.difference(['dt_op'])
df[cols] = df[cols].mask(df[cols] == 0).ffill().astype(int)
Similar solution with numpy.where:
df[cols] = pd.DataFrame(np.where(df[cols] == 0, np.nan, df[cols]),
index=df.index,
columns=cols).ffill().astype(int)
print (df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
Solution for fun - convert to integer all columns without dt_op:
d = dict.fromkeys(df.columns.difference(['dt_op']), 'int')
df = df.mask(df == 0).ffill().astype(d)
Idea is replace 0 to NaNs by mask and then forward filling them by previous non missing values:
cols = df.columns.difference(['dt_op'])
df[cols] = df[cols].mask(df[cols] == 0).ffill().astype(int)
Similar solution with numpy.where:
df[cols] = pd.DataFrame(np.where(df[cols] == 0, np.nan, df[cols]),
index=df.index,
columns=cols).ffill().astype(int)
print (df)
dt_op Prod_1 Prod_2 Prod_n
1 10/09/18 5 50 2
2 11/09/18 4 50 2
3 12/09/18 2 50 2
4 13/09/18 2 50 2
5 14/09/18 4 30 1
Solution for fun - convert to integer all columns without dt_op:
d = dict.fromkeys(df.columns.difference(['dt_op']), 'int')
df = df.mask(df == 0).ffill().astype(d)
edited Nov 23 '18 at 9:53
answered Nov 23 '18 at 9:47
jezraeljezrael
323k23265342
323k23265342
1
Another excellent piece +1
– pygo
Nov 23 '18 at 10:45
What if it reports: ValueError: Cannot convert non-finite values (NA or inf) to integer? @jezrael
– Alessandro Ceccarelli
Nov 23 '18 at 14:16
@AlessandroCeccarelli - It means there is someNaNvalue, so only remove.astype(int)
– jezrael
Nov 23 '18 at 14:17
@AlessandroCeccarelli - BecauseNaNisfloattype and cannot be casted to integers, so raise error
– jezrael
Nov 23 '18 at 14:18
1
Now it is working properly! Many thanks
– Alessandro Ceccarelli
Nov 23 '18 at 14:35
|
show 6 more comments
1
Another excellent piece +1
– pygo
Nov 23 '18 at 10:45
What if it reports: ValueError: Cannot convert non-finite values (NA or inf) to integer? @jezrael
– Alessandro Ceccarelli
Nov 23 '18 at 14:16
@AlessandroCeccarelli - It means there is someNaNvalue, so only remove.astype(int)
– jezrael
Nov 23 '18 at 14:17
@AlessandroCeccarelli - BecauseNaNisfloattype and cannot be casted to integers, so raise error
– jezrael
Nov 23 '18 at 14:18
1
Now it is working properly! Many thanks
– Alessandro Ceccarelli
Nov 23 '18 at 14:35
1
1
Another excellent piece +1
– pygo
Nov 23 '18 at 10:45
Another excellent piece +1
– pygo
Nov 23 '18 at 10:45
What if it reports: ValueError: Cannot convert non-finite values (NA or inf) to integer? @jezrael
– Alessandro Ceccarelli
Nov 23 '18 at 14:16
What if it reports: ValueError: Cannot convert non-finite values (NA or inf) to integer? @jezrael
– Alessandro Ceccarelli
Nov 23 '18 at 14:16
@AlessandroCeccarelli - It means there is some
NaN value, so only remove .astype(int)– jezrael
Nov 23 '18 at 14:17
@AlessandroCeccarelli - It means there is some
NaN value, so only remove .astype(int)– jezrael
Nov 23 '18 at 14:17
@AlessandroCeccarelli - Because
NaN is float type and cannot be casted to integers, so raise error– jezrael
Nov 23 '18 at 14:18
@AlessandroCeccarelli - Because
NaN is float type and cannot be casted to integers, so raise error– jezrael
Nov 23 '18 at 14:18
1
1
Now it is working properly! Many thanks
– Alessandro Ceccarelli
Nov 23 '18 at 14:35
Now it is working properly! Many thanks
– Alessandro Ceccarelli
Nov 23 '18 at 14:35
|
show 6 more comments
Just another option:
df.iloc[:,1:] = df.iloc[:,1:].replace(0, np.nan).ffill().astype(int)
1
This is an excellent show with slicing +1
– pygo
Nov 23 '18 at 10:46
add a comment |
Just another option:
df.iloc[:,1:] = df.iloc[:,1:].replace(0, np.nan).ffill().astype(int)
1
This is an excellent show with slicing +1
– pygo
Nov 23 '18 at 10:46
add a comment |
Just another option:
df.iloc[:,1:] = df.iloc[:,1:].replace(0, np.nan).ffill().astype(int)
Just another option:
df.iloc[:,1:] = df.iloc[:,1:].replace(0, np.nan).ffill().astype(int)
edited Nov 23 '18 at 10:47
answered Nov 23 '18 at 10:22
JoeJoe
5,89621129
5,89621129
1
This is an excellent show with slicing +1
– pygo
Nov 23 '18 at 10:46
add a comment |
1
This is an excellent show with slicing +1
– pygo
Nov 23 '18 at 10:46
1
1
This is an excellent show with slicing +1
– pygo
Nov 23 '18 at 10:46
This is an excellent show with slicing +1
– pygo
Nov 23 '18 at 10:46
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%2f53444173%2freplace-zeros-with-last-value-different-from-zero%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