How do you shift each row in pandas data frame by a specific value?












0














If I have a pandas dataframe like this:



 2  3   4  NaN NaN  NaN
1 NaN NaN NaN NaN NaN
5 6 7 2 3 NaN
4 3 NaN NaN NaN NaN


and an array for the number I would like to shift:
array = [2, 4, 0, 3]



How do I iterate through each row to shift the columns by the number in my array to get something like this:



 NaN  NaN   2   3   4   NaN
NaN NaN NaN NaN 1 NaN
5 6 7 2 3 NaN
NaN NaN NaN 3 4 NaN


I was trying to do something like this but had no luck.



 df = pd.DataFrame(values)
for rows in df.iterrows():
df[rows] = df.shift[change_in_bins[rows]]









share|improve this question






















  • Is the last column always NaN?
    – coldspeed
    Nov 12 '18 at 4:48
















0














If I have a pandas dataframe like this:



 2  3   4  NaN NaN  NaN
1 NaN NaN NaN NaN NaN
5 6 7 2 3 NaN
4 3 NaN NaN NaN NaN


and an array for the number I would like to shift:
array = [2, 4, 0, 3]



How do I iterate through each row to shift the columns by the number in my array to get something like this:



 NaN  NaN   2   3   4   NaN
NaN NaN NaN NaN 1 NaN
5 6 7 2 3 NaN
NaN NaN NaN 3 4 NaN


I was trying to do something like this but had no luck.



 df = pd.DataFrame(values)
for rows in df.iterrows():
df[rows] = df.shift[change_in_bins[rows]]









share|improve this question






















  • Is the last column always NaN?
    – coldspeed
    Nov 12 '18 at 4:48














0












0








0







If I have a pandas dataframe like this:



 2  3   4  NaN NaN  NaN
1 NaN NaN NaN NaN NaN
5 6 7 2 3 NaN
4 3 NaN NaN NaN NaN


and an array for the number I would like to shift:
array = [2, 4, 0, 3]



How do I iterate through each row to shift the columns by the number in my array to get something like this:



 NaN  NaN   2   3   4   NaN
NaN NaN NaN NaN 1 NaN
5 6 7 2 3 NaN
NaN NaN NaN 3 4 NaN


I was trying to do something like this but had no luck.



 df = pd.DataFrame(values)
for rows in df.iterrows():
df[rows] = df.shift[change_in_bins[rows]]









share|improve this question













If I have a pandas dataframe like this:



 2  3   4  NaN NaN  NaN
1 NaN NaN NaN NaN NaN
5 6 7 2 3 NaN
4 3 NaN NaN NaN NaN


and an array for the number I would like to shift:
array = [2, 4, 0, 3]



How do I iterate through each row to shift the columns by the number in my array to get something like this:



 NaN  NaN   2   3   4   NaN
NaN NaN NaN NaN 1 NaN
5 6 7 2 3 NaN
NaN NaN NaN 3 4 NaN


I was trying to do something like this but had no luck.



 df = pd.DataFrame(values)
for rows in df.iterrows():
df[rows] = df.shift[change_in_bins[rows]]






python python-2.7 pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 4:37









Troy Zaremba

785




785












  • Is the last column always NaN?
    – coldspeed
    Nov 12 '18 at 4:48


















  • Is the last column always NaN?
    – coldspeed
    Nov 12 '18 at 4:48
















Is the last column always NaN?
– coldspeed
Nov 12 '18 at 4:48




Is the last column always NaN?
– coldspeed
Nov 12 '18 at 4:48












1 Answer
1






active

oldest

votes


















2














Use for loop with loc and shift:



for index,value in enumerate([2, 4, 0, 3]):
df.loc[index,:] = df.loc[index,:].shift(value)

print(df)
0 1 2 3 4 5
0 NaN NaN 2.0 3.0 4.0 NaN
1 NaN NaN NaN NaN 1.0 NaN
2 5.0 6.0 7.0 2.0 3.0 NaN
3 NaN NaN NaN 4.0 3.0 NaN





share|improve this answer



















  • 1




    Thank you so much!
    – Troy Zaremba
    Nov 12 '18 at 14:17










  • @TroyZaremba You are Welcome!
    – Sandeep Kadapa
    Nov 12 '18 at 15:42











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256038%2fhow-do-you-shift-each-row-in-pandas-data-frame-by-a-specific-value%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









2














Use for loop with loc and shift:



for index,value in enumerate([2, 4, 0, 3]):
df.loc[index,:] = df.loc[index,:].shift(value)

print(df)
0 1 2 3 4 5
0 NaN NaN 2.0 3.0 4.0 NaN
1 NaN NaN NaN NaN 1.0 NaN
2 5.0 6.0 7.0 2.0 3.0 NaN
3 NaN NaN NaN 4.0 3.0 NaN





share|improve this answer



















  • 1




    Thank you so much!
    – Troy Zaremba
    Nov 12 '18 at 14:17










  • @TroyZaremba You are Welcome!
    – Sandeep Kadapa
    Nov 12 '18 at 15:42
















2














Use for loop with loc and shift:



for index,value in enumerate([2, 4, 0, 3]):
df.loc[index,:] = df.loc[index,:].shift(value)

print(df)
0 1 2 3 4 5
0 NaN NaN 2.0 3.0 4.0 NaN
1 NaN NaN NaN NaN 1.0 NaN
2 5.0 6.0 7.0 2.0 3.0 NaN
3 NaN NaN NaN 4.0 3.0 NaN





share|improve this answer



















  • 1




    Thank you so much!
    – Troy Zaremba
    Nov 12 '18 at 14:17










  • @TroyZaremba You are Welcome!
    – Sandeep Kadapa
    Nov 12 '18 at 15:42














2












2








2






Use for loop with loc and shift:



for index,value in enumerate([2, 4, 0, 3]):
df.loc[index,:] = df.loc[index,:].shift(value)

print(df)
0 1 2 3 4 5
0 NaN NaN 2.0 3.0 4.0 NaN
1 NaN NaN NaN NaN 1.0 NaN
2 5.0 6.0 7.0 2.0 3.0 NaN
3 NaN NaN NaN 4.0 3.0 NaN





share|improve this answer














Use for loop with loc and shift:



for index,value in enumerate([2, 4, 0, 3]):
df.loc[index,:] = df.loc[index,:].shift(value)

print(df)
0 1 2 3 4 5
0 NaN NaN 2.0 3.0 4.0 NaN
1 NaN NaN NaN NaN 1.0 NaN
2 5.0 6.0 7.0 2.0 3.0 NaN
3 NaN NaN NaN 4.0 3.0 NaN






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 4:52

























answered Nov 12 '18 at 4:44









Sandeep Kadapa

5,947428




5,947428








  • 1




    Thank you so much!
    – Troy Zaremba
    Nov 12 '18 at 14:17










  • @TroyZaremba You are Welcome!
    – Sandeep Kadapa
    Nov 12 '18 at 15:42














  • 1




    Thank you so much!
    – Troy Zaremba
    Nov 12 '18 at 14:17










  • @TroyZaremba You are Welcome!
    – Sandeep Kadapa
    Nov 12 '18 at 15:42








1




1




Thank you so much!
– Troy Zaremba
Nov 12 '18 at 14:17




Thank you so much!
– Troy Zaremba
Nov 12 '18 at 14:17












@TroyZaremba You are Welcome!
– Sandeep Kadapa
Nov 12 '18 at 15:42




@TroyZaremba You are Welcome!
– Sandeep Kadapa
Nov 12 '18 at 15:42


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256038%2fhow-do-you-shift-each-row-in-pandas-data-frame-by-a-specific-value%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings