setting up a new layout with unique values of multiple columns from a data frame in python










0















I have the following data frame:



df = pd.DataFrame( 'group' : ["A","A","A","B","B","B"],
'unit_name' : ["BA234","BA744","BA240","BB237","BB740","BB27"],
'value' :["50","10","180","10","10","660"],
'set0' :["Country","Country","Country","Country","Country","Country"],
'set_1' :["size1","size1","size2","size3","size4","size3"],
'set_2' :["size12","size12","size12","size9","size13","size13"],
'set_3' :["size14","size14","size15","NO","NO","NO"],
'set_4' :["NO","NO","NO","Size25","Size25","Size27"],
'set_5' :["NO","NO","NO","NO","NO","NO"]
)


Without hard coding:



I want to get a new resulting data frame/layout so that each value of the "group" column is repeated with the number unique values of "set_" related columns ("NO" values should be disregarded) and align horizontally.



Since I am not able to share any image of the desired output, I am sharing the transposed form of it.



The resulting data frame should look like the transposed form of the following:



df_new = pd.DataFrame( 'Group' : ["A","A","A","A","A","A","B","B","B","B","B","B","B"],
'sets' : ["Country","Size1","Size2","Size12","Size14","Size15","Country","Size3","Size4","Size9","Size13","Size25","Size27"],
)


Could you please help regarding the issue?










share|improve this question

















  • 1





    Why not show us the exact desired output rather than a transposed version? Or is df_new exactly what you want?

    – jpp
    Nov 12 '18 at 16:59
















0















I have the following data frame:



df = pd.DataFrame( 'group' : ["A","A","A","B","B","B"],
'unit_name' : ["BA234","BA744","BA240","BB237","BB740","BB27"],
'value' :["50","10","180","10","10","660"],
'set0' :["Country","Country","Country","Country","Country","Country"],
'set_1' :["size1","size1","size2","size3","size4","size3"],
'set_2' :["size12","size12","size12","size9","size13","size13"],
'set_3' :["size14","size14","size15","NO","NO","NO"],
'set_4' :["NO","NO","NO","Size25","Size25","Size27"],
'set_5' :["NO","NO","NO","NO","NO","NO"]
)


Without hard coding:



I want to get a new resulting data frame/layout so that each value of the "group" column is repeated with the number unique values of "set_" related columns ("NO" values should be disregarded) and align horizontally.



Since I am not able to share any image of the desired output, I am sharing the transposed form of it.



The resulting data frame should look like the transposed form of the following:



df_new = pd.DataFrame( 'Group' : ["A","A","A","A","A","A","B","B","B","B","B","B","B"],
'sets' : ["Country","Size1","Size2","Size12","Size14","Size15","Country","Size3","Size4","Size9","Size13","Size25","Size27"],
)


Could you please help regarding the issue?










share|improve this question

















  • 1





    Why not show us the exact desired output rather than a transposed version? Or is df_new exactly what you want?

    – jpp
    Nov 12 '18 at 16:59














0












0








0








I have the following data frame:



df = pd.DataFrame( 'group' : ["A","A","A","B","B","B"],
'unit_name' : ["BA234","BA744","BA240","BB237","BB740","BB27"],
'value' :["50","10","180","10","10","660"],
'set0' :["Country","Country","Country","Country","Country","Country"],
'set_1' :["size1","size1","size2","size3","size4","size3"],
'set_2' :["size12","size12","size12","size9","size13","size13"],
'set_3' :["size14","size14","size15","NO","NO","NO"],
'set_4' :["NO","NO","NO","Size25","Size25","Size27"],
'set_5' :["NO","NO","NO","NO","NO","NO"]
)


Without hard coding:



I want to get a new resulting data frame/layout so that each value of the "group" column is repeated with the number unique values of "set_" related columns ("NO" values should be disregarded) and align horizontally.



Since I am not able to share any image of the desired output, I am sharing the transposed form of it.



The resulting data frame should look like the transposed form of the following:



df_new = pd.DataFrame( 'Group' : ["A","A","A","A","A","A","B","B","B","B","B","B","B"],
'sets' : ["Country","Size1","Size2","Size12","Size14","Size15","Country","Size3","Size4","Size9","Size13","Size25","Size27"],
)


Could you please help regarding the issue?










share|improve this question














I have the following data frame:



df = pd.DataFrame( 'group' : ["A","A","A","B","B","B"],
'unit_name' : ["BA234","BA744","BA240","BB237","BB740","BB27"],
'value' :["50","10","180","10","10","660"],
'set0' :["Country","Country","Country","Country","Country","Country"],
'set_1' :["size1","size1","size2","size3","size4","size3"],
'set_2' :["size12","size12","size12","size9","size13","size13"],
'set_3' :["size14","size14","size15","NO","NO","NO"],
'set_4' :["NO","NO","NO","Size25","Size25","Size27"],
'set_5' :["NO","NO","NO","NO","NO","NO"]
)


Without hard coding:



I want to get a new resulting data frame/layout so that each value of the "group" column is repeated with the number unique values of "set_" related columns ("NO" values should be disregarded) and align horizontally.



Since I am not able to share any image of the desired output, I am sharing the transposed form of it.



The resulting data frame should look like the transposed form of the following:



df_new = pd.DataFrame( 'Group' : ["A","A","A","A","A","A","B","B","B","B","B","B","B"],
'sets' : ["Country","Size1","Size2","Size12","Size14","Size15","Country","Size3","Size4","Size9","Size13","Size25","Size27"],
)


Could you please help regarding the issue?







python-3.x pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 16:54









nkltkfnkltkf

113




113







  • 1





    Why not show us the exact desired output rather than a transposed version? Or is df_new exactly what you want?

    – jpp
    Nov 12 '18 at 16:59













  • 1





    Why not show us the exact desired output rather than a transposed version? Or is df_new exactly what you want?

    – jpp
    Nov 12 '18 at 16:59








1




1





Why not show us the exact desired output rather than a transposed version? Or is df_new exactly what you want?

– jpp
Nov 12 '18 at 16:59






Why not show us the exact desired output rather than a transposed version? Or is df_new exactly what you want?

– jpp
Nov 12 '18 at 16:59













2 Answers
2






active

oldest

votes


















2














You can use the following, which stacks your dataframe, filtered where the columns have set in the name, and drops the duplicates:



df_new = (df.set_index('group').replace('NO',np.nan)
.filter(like='set').stack()
.reset_index('group')
.drop_duplicates())

>>> df_new
group 0
set0 A Country
set_1 A size1
set_2 A size12
set_3 A size14
set_1 A size2
set_3 A size15
set0 B Country
set_1 B size3
set_2 B size9
set_4 B Size25
set_1 B size4
set_2 B size13
set_4 B Size27





share|improve this answer























  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30



















1














With pd.melt



import pandas as pd

(pd.melt(df,
id_vars='group',
value_name='Country', value_vars=[x for x in df.columns if 'set' in x])
.drop(columns='variable')
.drop_duplicates()
.sort_values('group')
.query('Country != "NO"')
)


Output:



 group Country
0 A Country
6 A size1
8 A size2
12 A size12
18 A size14
20 A size15
3 B Country
9 B size3
10 B size4
15 B size9
16 B size13
27 B Size25
29 B Size27





share|improve this answer

























  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30











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%2f53266749%2fsetting-up-a-new-layout-with-unique-values-of-multiple-columns-from-a-data-frame%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









2














You can use the following, which stacks your dataframe, filtered where the columns have set in the name, and drops the duplicates:



df_new = (df.set_index('group').replace('NO',np.nan)
.filter(like='set').stack()
.reset_index('group')
.drop_duplicates())

>>> df_new
group 0
set0 A Country
set_1 A size1
set_2 A size12
set_3 A size14
set_1 A size2
set_3 A size15
set0 B Country
set_1 B size3
set_2 B size9
set_4 B Size25
set_1 B size4
set_2 B size13
set_4 B Size27





share|improve this answer























  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30
















2














You can use the following, which stacks your dataframe, filtered where the columns have set in the name, and drops the duplicates:



df_new = (df.set_index('group').replace('NO',np.nan)
.filter(like='set').stack()
.reset_index('group')
.drop_duplicates())

>>> df_new
group 0
set0 A Country
set_1 A size1
set_2 A size12
set_3 A size14
set_1 A size2
set_3 A size15
set0 B Country
set_1 B size3
set_2 B size9
set_4 B Size25
set_1 B size4
set_2 B size13
set_4 B Size27





share|improve this answer























  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30














2












2








2







You can use the following, which stacks your dataframe, filtered where the columns have set in the name, and drops the duplicates:



df_new = (df.set_index('group').replace('NO',np.nan)
.filter(like='set').stack()
.reset_index('group')
.drop_duplicates())

>>> df_new
group 0
set0 A Country
set_1 A size1
set_2 A size12
set_3 A size14
set_1 A size2
set_3 A size15
set0 B Country
set_1 B size3
set_2 B size9
set_4 B Size25
set_1 B size4
set_2 B size13
set_4 B Size27





share|improve this answer













You can use the following, which stacks your dataframe, filtered where the columns have set in the name, and drops the duplicates:



df_new = (df.set_index('group').replace('NO',np.nan)
.filter(like='set').stack()
.reset_index('group')
.drop_duplicates())

>>> df_new
group 0
set0 A Country
set_1 A size1
set_2 A size12
set_3 A size14
set_1 A size2
set_3 A size15
set0 B Country
set_1 B size3
set_2 B size9
set_4 B Size25
set_1 B size4
set_2 B size13
set_4 B Size27






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 17:03









saculsacul

30k41740




30k41740












  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30


















  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30

















Thanks a lot,works perfectly!

– nkltkf
Nov 12 '18 at 18:30






Thanks a lot,works perfectly!

– nkltkf
Nov 12 '18 at 18:30














1














With pd.melt



import pandas as pd

(pd.melt(df,
id_vars='group',
value_name='Country', value_vars=[x for x in df.columns if 'set' in x])
.drop(columns='variable')
.drop_duplicates()
.sort_values('group')
.query('Country != "NO"')
)


Output:



 group Country
0 A Country
6 A size1
8 A size2
12 A size12
18 A size14
20 A size15
3 B Country
9 B size3
10 B size4
15 B size9
16 B size13
27 B Size25
29 B Size27





share|improve this answer

























  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30
















1














With pd.melt



import pandas as pd

(pd.melt(df,
id_vars='group',
value_name='Country', value_vars=[x for x in df.columns if 'set' in x])
.drop(columns='variable')
.drop_duplicates()
.sort_values('group')
.query('Country != "NO"')
)


Output:



 group Country
0 A Country
6 A size1
8 A size2
12 A size12
18 A size14
20 A size15
3 B Country
9 B size3
10 B size4
15 B size9
16 B size13
27 B Size25
29 B Size27





share|improve this answer

























  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30














1












1








1







With pd.melt



import pandas as pd

(pd.melt(df,
id_vars='group',
value_name='Country', value_vars=[x for x in df.columns if 'set' in x])
.drop(columns='variable')
.drop_duplicates()
.sort_values('group')
.query('Country != "NO"')
)


Output:



 group Country
0 A Country
6 A size1
8 A size2
12 A size12
18 A size14
20 A size15
3 B Country
9 B size3
10 B size4
15 B size9
16 B size13
27 B Size25
29 B Size27





share|improve this answer















With pd.melt



import pandas as pd

(pd.melt(df,
id_vars='group',
value_name='Country', value_vars=[x for x in df.columns if 'set' in x])
.drop(columns='variable')
.drop_duplicates()
.sort_values('group')
.query('Country != "NO"')
)


Output:



 group Country
0 A Country
6 A size1
8 A size2
12 A size12
18 A size14
20 A size15
3 B Country
9 B size3
10 B size4
15 B size9
16 B size13
27 B Size25
29 B Size27






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 17:15

























answered Nov 12 '18 at 17:04









ALollzALollz

11.7k31436




11.7k31436












  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30


















  • Thanks a lot,works perfectly!

    – nkltkf
    Nov 12 '18 at 18:30

















Thanks a lot,works perfectly!

– nkltkf
Nov 12 '18 at 18:30






Thanks a lot,works perfectly!

– nkltkf
Nov 12 '18 at 18:30


















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%2f53266749%2fsetting-up-a-new-layout-with-unique-values-of-multiple-columns-from-a-data-frame%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

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20