Convert Columns to Rows every nth value in pandas and join with a comma












0















Here is what I have:



data = np.array([np.arange(1000)]*2).T

df = pd.DataFrame(data,columns=['a','b'])



a b
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
... ... ...
995 995 995
996 996 996
997 997 997
998 998 998
999 999 999


Here is what I am trying to do:



For column a, take the first 100th rows, append each row with a comma, add them as a row of their own. Take the next 100 rows. Continue till you exhaust the points in the column.
Repeat for column b



Overwriting the existing column, or creating a new column does not matter.



Here is what I am trying to make it look like:



       c                   d
0,1,2,3,4,5...99 0,1,2,3,4,5...99
101, 102, 103,... 101, 102, 103,...









share|improve this question


















  • 2





    Does df.astype(str).groupby(df.index//100).agg(', '.join) do what you want?

    – Jon Clements
    Nov 21 '18 at 15:42











  • @JonClements yes this works to an extent, but breaks if the number of rows is 110 for example. Can it be robust against the number of rows? so it can work for 110, one row will have 100, the next will have 10?

    – Matthew
    Nov 21 '18 at 15:48











  • @JonClements sorry! Disregard my comment for a second !

    – Matthew
    Nov 21 '18 at 15:50











  • @JonClements yes this works perfectly ! Thank you. Please add it as an answer

    – Matthew
    Nov 21 '18 at 15:56
















0















Here is what I have:



data = np.array([np.arange(1000)]*2).T

df = pd.DataFrame(data,columns=['a','b'])



a b
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
... ... ...
995 995 995
996 996 996
997 997 997
998 998 998
999 999 999


Here is what I am trying to do:



For column a, take the first 100th rows, append each row with a comma, add them as a row of their own. Take the next 100 rows. Continue till you exhaust the points in the column.
Repeat for column b



Overwriting the existing column, or creating a new column does not matter.



Here is what I am trying to make it look like:



       c                   d
0,1,2,3,4,5...99 0,1,2,3,4,5...99
101, 102, 103,... 101, 102, 103,...









share|improve this question


















  • 2





    Does df.astype(str).groupby(df.index//100).agg(', '.join) do what you want?

    – Jon Clements
    Nov 21 '18 at 15:42











  • @JonClements yes this works to an extent, but breaks if the number of rows is 110 for example. Can it be robust against the number of rows? so it can work for 110, one row will have 100, the next will have 10?

    – Matthew
    Nov 21 '18 at 15:48











  • @JonClements sorry! Disregard my comment for a second !

    – Matthew
    Nov 21 '18 at 15:50











  • @JonClements yes this works perfectly ! Thank you. Please add it as an answer

    – Matthew
    Nov 21 '18 at 15:56














0












0








0








Here is what I have:



data = np.array([np.arange(1000)]*2).T

df = pd.DataFrame(data,columns=['a','b'])



a b
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
... ... ...
995 995 995
996 996 996
997 997 997
998 998 998
999 999 999


Here is what I am trying to do:



For column a, take the first 100th rows, append each row with a comma, add them as a row of their own. Take the next 100 rows. Continue till you exhaust the points in the column.
Repeat for column b



Overwriting the existing column, or creating a new column does not matter.



Here is what I am trying to make it look like:



       c                   d
0,1,2,3,4,5...99 0,1,2,3,4,5...99
101, 102, 103,... 101, 102, 103,...









share|improve this question














Here is what I have:



data = np.array([np.arange(1000)]*2).T

df = pd.DataFrame(data,columns=['a','b'])



a b
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
... ... ...
995 995 995
996 996 996
997 997 997
998 998 998
999 999 999


Here is what I am trying to do:



For column a, take the first 100th rows, append each row with a comma, add them as a row of their own. Take the next 100 rows. Continue till you exhaust the points in the column.
Repeat for column b



Overwriting the existing column, or creating a new column does not matter.



Here is what I am trying to make it look like:



       c                   d
0,1,2,3,4,5...99 0,1,2,3,4,5...99
101, 102, 103,... 101, 102, 103,...






python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 15:37









MatthewMatthew

89114




89114








  • 2





    Does df.astype(str).groupby(df.index//100).agg(', '.join) do what you want?

    – Jon Clements
    Nov 21 '18 at 15:42











  • @JonClements yes this works to an extent, but breaks if the number of rows is 110 for example. Can it be robust against the number of rows? so it can work for 110, one row will have 100, the next will have 10?

    – Matthew
    Nov 21 '18 at 15:48











  • @JonClements sorry! Disregard my comment for a second !

    – Matthew
    Nov 21 '18 at 15:50











  • @JonClements yes this works perfectly ! Thank you. Please add it as an answer

    – Matthew
    Nov 21 '18 at 15:56














  • 2





    Does df.astype(str).groupby(df.index//100).agg(', '.join) do what you want?

    – Jon Clements
    Nov 21 '18 at 15:42











  • @JonClements yes this works to an extent, but breaks if the number of rows is 110 for example. Can it be robust against the number of rows? so it can work for 110, one row will have 100, the next will have 10?

    – Matthew
    Nov 21 '18 at 15:48











  • @JonClements sorry! Disregard my comment for a second !

    – Matthew
    Nov 21 '18 at 15:50











  • @JonClements yes this works perfectly ! Thank you. Please add it as an answer

    – Matthew
    Nov 21 '18 at 15:56








2




2





Does df.astype(str).groupby(df.index//100).agg(', '.join) do what you want?

– Jon Clements
Nov 21 '18 at 15:42





Does df.astype(str).groupby(df.index//100).agg(', '.join) do what you want?

– Jon Clements
Nov 21 '18 at 15:42













@JonClements yes this works to an extent, but breaks if the number of rows is 110 for example. Can it be robust against the number of rows? so it can work for 110, one row will have 100, the next will have 10?

– Matthew
Nov 21 '18 at 15:48





@JonClements yes this works to an extent, but breaks if the number of rows is 110 for example. Can it be robust against the number of rows? so it can work for 110, one row will have 100, the next will have 10?

– Matthew
Nov 21 '18 at 15:48













@JonClements sorry! Disregard my comment for a second !

– Matthew
Nov 21 '18 at 15:50





@JonClements sorry! Disregard my comment for a second !

– Matthew
Nov 21 '18 at 15:50













@JonClements yes this works perfectly ! Thank you. Please add it as an answer

– Matthew
Nov 21 '18 at 15:56





@JonClements yes this works perfectly ! Thank you. Please add it as an answer

– Matthew
Nov 21 '18 at 15:56












1 Answer
1






active

oldest

votes


















1














Here's something you could do:



df = df.assign(cut=pd.cut(x=df.a, bins = np.arange(0, len(data)+2, 100)))


And for a you could do:



df['c'] = df.groupby('cut')['a'].transform(lambda x:  ','.join(map(str, x.values.tolist())))
df.drop('cut', axis = 1)

a b c
0 1 1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
1 2 2 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
2 3 3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
3 4 4 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
4 5 5 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...





share|improve this answer
























  • Albeit if @JonClements answer does work it is a more straight forward way of doing it

    – yatu
    Nov 21 '18 at 16:13











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%2f53415560%2fconvert-columns-to-rows-every-nth-value-in-pandas-and-join-with-a-comma%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









1














Here's something you could do:



df = df.assign(cut=pd.cut(x=df.a, bins = np.arange(0, len(data)+2, 100)))


And for a you could do:



df['c'] = df.groupby('cut')['a'].transform(lambda x:  ','.join(map(str, x.values.tolist())))
df.drop('cut', axis = 1)

a b c
0 1 1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
1 2 2 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
2 3 3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
3 4 4 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
4 5 5 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...





share|improve this answer
























  • Albeit if @JonClements answer does work it is a more straight forward way of doing it

    – yatu
    Nov 21 '18 at 16:13
















1














Here's something you could do:



df = df.assign(cut=pd.cut(x=df.a, bins = np.arange(0, len(data)+2, 100)))


And for a you could do:



df['c'] = df.groupby('cut')['a'].transform(lambda x:  ','.join(map(str, x.values.tolist())))
df.drop('cut', axis = 1)

a b c
0 1 1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
1 2 2 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
2 3 3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
3 4 4 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
4 5 5 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...





share|improve this answer
























  • Albeit if @JonClements answer does work it is a more straight forward way of doing it

    – yatu
    Nov 21 '18 at 16:13














1












1








1







Here's something you could do:



df = df.assign(cut=pd.cut(x=df.a, bins = np.arange(0, len(data)+2, 100)))


And for a you could do:



df['c'] = df.groupby('cut')['a'].transform(lambda x:  ','.join(map(str, x.values.tolist())))
df.drop('cut', axis = 1)

a b c
0 1 1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
1 2 2 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
2 3 3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
3 4 4 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
4 5 5 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...





share|improve this answer













Here's something you could do:



df = df.assign(cut=pd.cut(x=df.a, bins = np.arange(0, len(data)+2, 100)))


And for a you could do:



df['c'] = df.groupby('cut')['a'].transform(lambda x:  ','.join(map(str, x.values.tolist())))
df.drop('cut', axis = 1)

a b c
0 1 1 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
1 2 2 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
2 3 3 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
3 4 4 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...
4 5 5 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1...






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 16:10









yatuyatu

9,55611032




9,55611032













  • Albeit if @JonClements answer does work it is a more straight forward way of doing it

    – yatu
    Nov 21 '18 at 16:13



















  • Albeit if @JonClements answer does work it is a more straight forward way of doing it

    – yatu
    Nov 21 '18 at 16:13

















Albeit if @JonClements answer does work it is a more straight forward way of doing it

– yatu
Nov 21 '18 at 16:13





Albeit if @JonClements answer does work it is a more straight forward way of doing it

– yatu
Nov 21 '18 at 16:13




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415560%2fconvert-columns-to-rows-every-nth-value-in-pandas-and-join-with-a-comma%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







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window