is it possible to split column from specific element in list?












1















I have tried to transform the data frame into the list like this.



here is the data frame before transformation.



df_gr = df_com.groupby(['Publication_Year_x', 'UPC', 'CPC']).size().unstack(fill_value=0)
df_gr_peryear = df_gr.replace(0, '', regex=True)

df_gr_peryear


the data frame



and here is the list that i have tried with this code



list_allyear = [(list(i), v) for i, v in df_gr_peryear.stack().iteritems()]
list_allyear = pd.DataFrame(list_allyear, columns=["MOC", "count"])
list_allyear


here is the result



enter image description here



but I would like to split it just only the first element in all rows. the expected result will be



year  MOC         count
1971 [136,-]
1971 [136, A01D]
.
.
1972 [231, H01L] 5


I have tried the pd.DataFrame(list_allyear.MOC.values.tolist()) but it splits all elements that contain in the rows










share|improve this question























  • One question - Is not problem mixed empty values with numeric? Not better omit df_gr_peryear = df_gr.replace(0, '', regex=True) ?

    – jezrael
    Nov 22 '18 at 6:55











  • yes, there is not the problem

    – Hook Im
    Nov 22 '18 at 6:56
















1















I have tried to transform the data frame into the list like this.



here is the data frame before transformation.



df_gr = df_com.groupby(['Publication_Year_x', 'UPC', 'CPC']).size().unstack(fill_value=0)
df_gr_peryear = df_gr.replace(0, '', regex=True)

df_gr_peryear


the data frame



and here is the list that i have tried with this code



list_allyear = [(list(i), v) for i, v in df_gr_peryear.stack().iteritems()]
list_allyear = pd.DataFrame(list_allyear, columns=["MOC", "count"])
list_allyear


here is the result



enter image description here



but I would like to split it just only the first element in all rows. the expected result will be



year  MOC         count
1971 [136,-]
1971 [136, A01D]
.
.
1972 [231, H01L] 5


I have tried the pd.DataFrame(list_allyear.MOC.values.tolist()) but it splits all elements that contain in the rows










share|improve this question























  • One question - Is not problem mixed empty values with numeric? Not better omit df_gr_peryear = df_gr.replace(0, '', regex=True) ?

    – jezrael
    Nov 22 '18 at 6:55











  • yes, there is not the problem

    – Hook Im
    Nov 22 '18 at 6:56














1












1








1








I have tried to transform the data frame into the list like this.



here is the data frame before transformation.



df_gr = df_com.groupby(['Publication_Year_x', 'UPC', 'CPC']).size().unstack(fill_value=0)
df_gr_peryear = df_gr.replace(0, '', regex=True)

df_gr_peryear


the data frame



and here is the list that i have tried with this code



list_allyear = [(list(i), v) for i, v in df_gr_peryear.stack().iteritems()]
list_allyear = pd.DataFrame(list_allyear, columns=["MOC", "count"])
list_allyear


here is the result



enter image description here



but I would like to split it just only the first element in all rows. the expected result will be



year  MOC         count
1971 [136,-]
1971 [136, A01D]
.
.
1972 [231, H01L] 5


I have tried the pd.DataFrame(list_allyear.MOC.values.tolist()) but it splits all elements that contain in the rows










share|improve this question














I have tried to transform the data frame into the list like this.



here is the data frame before transformation.



df_gr = df_com.groupby(['Publication_Year_x', 'UPC', 'CPC']).size().unstack(fill_value=0)
df_gr_peryear = df_gr.replace(0, '', regex=True)

df_gr_peryear


the data frame



and here is the list that i have tried with this code



list_allyear = [(list(i), v) for i, v in df_gr_peryear.stack().iteritems()]
list_allyear = pd.DataFrame(list_allyear, columns=["MOC", "count"])
list_allyear


here is the result



enter image description here



but I would like to split it just only the first element in all rows. the expected result will be



year  MOC         count
1971 [136,-]
1971 [136, A01D]
.
.
1972 [231, H01L] 5


I have tried the pd.DataFrame(list_allyear.MOC.values.tolist()) but it splits all elements that contain in the rows







python pandas list






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 6:38









Hook ImHook Im

1318




1318













  • One question - Is not problem mixed empty values with numeric? Not better omit df_gr_peryear = df_gr.replace(0, '', regex=True) ?

    – jezrael
    Nov 22 '18 at 6:55











  • yes, there is not the problem

    – Hook Im
    Nov 22 '18 at 6:56



















  • One question - Is not problem mixed empty values with numeric? Not better omit df_gr_peryear = df_gr.replace(0, '', regex=True) ?

    – jezrael
    Nov 22 '18 at 6:55











  • yes, there is not the problem

    – Hook Im
    Nov 22 '18 at 6:56

















One question - Is not problem mixed empty values with numeric? Not better omit df_gr_peryear = df_gr.replace(0, '', regex=True) ?

– jezrael
Nov 22 '18 at 6:55





One question - Is not problem mixed empty values with numeric? Not better omit df_gr_peryear = df_gr.replace(0, '', regex=True) ?

– jezrael
Nov 22 '18 at 6:55













yes, there is not the problem

– Hook Im
Nov 22 '18 at 6:56





yes, there is not the problem

– Hook Im
Nov 22 '18 at 6:56












1 Answer
1






active

oldest

votes


















2














Just simplified your code where you can use fill_value='' instead replace and then creating a dictionaries for list comprehension to have a final DataFrame:



cols = ['Publication_Year_x', 'UPC', 'CPC']
s = df_com.groupby(cols).size().unstack(fill_value='').stack()

L = [{'year': idx[0], 'MOC': list(idx[1:]), 'count': vals} for idx, vals in s.items()]
list_allyear = pd.DataFrame(L)





share|improve this answer


























  • @HookIm - You are welcome! Be free upvote solution too :)

    – jezrael
    Nov 22 '18 at 7:00






  • 1





    @pygo - Thank you.

    – jezrael
    Nov 22 '18 at 7:03






  • 1





    Nice solution +1 :-) , something new to learn

    – pygo
    Nov 22 '18 at 7:04











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%2f53425167%2fis-it-possible-to-split-column-from-specific-element-in-list%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














Just simplified your code where you can use fill_value='' instead replace and then creating a dictionaries for list comprehension to have a final DataFrame:



cols = ['Publication_Year_x', 'UPC', 'CPC']
s = df_com.groupby(cols).size().unstack(fill_value='').stack()

L = [{'year': idx[0], 'MOC': list(idx[1:]), 'count': vals} for idx, vals in s.items()]
list_allyear = pd.DataFrame(L)





share|improve this answer


























  • @HookIm - You are welcome! Be free upvote solution too :)

    – jezrael
    Nov 22 '18 at 7:00






  • 1





    @pygo - Thank you.

    – jezrael
    Nov 22 '18 at 7:03






  • 1





    Nice solution +1 :-) , something new to learn

    – pygo
    Nov 22 '18 at 7:04
















2














Just simplified your code where you can use fill_value='' instead replace and then creating a dictionaries for list comprehension to have a final DataFrame:



cols = ['Publication_Year_x', 'UPC', 'CPC']
s = df_com.groupby(cols).size().unstack(fill_value='').stack()

L = [{'year': idx[0], 'MOC': list(idx[1:]), 'count': vals} for idx, vals in s.items()]
list_allyear = pd.DataFrame(L)





share|improve this answer


























  • @HookIm - You are welcome! Be free upvote solution too :)

    – jezrael
    Nov 22 '18 at 7:00






  • 1





    @pygo - Thank you.

    – jezrael
    Nov 22 '18 at 7:03






  • 1





    Nice solution +1 :-) , something new to learn

    – pygo
    Nov 22 '18 at 7:04














2












2








2







Just simplified your code where you can use fill_value='' instead replace and then creating a dictionaries for list comprehension to have a final DataFrame:



cols = ['Publication_Year_x', 'UPC', 'CPC']
s = df_com.groupby(cols).size().unstack(fill_value='').stack()

L = [{'year': idx[0], 'MOC': list(idx[1:]), 'count': vals} for idx, vals in s.items()]
list_allyear = pd.DataFrame(L)





share|improve this answer















Just simplified your code where you can use fill_value='' instead replace and then creating a dictionaries for list comprehension to have a final DataFrame:



cols = ['Publication_Year_x', 'UPC', 'CPC']
s = df_com.groupby(cols).size().unstack(fill_value='').stack()

L = [{'year': idx[0], 'MOC': list(idx[1:]), 'count': vals} for idx, vals in s.items()]
list_allyear = pd.DataFrame(L)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 7:03









pygo

3,1961619




3,1961619










answered Nov 22 '18 at 6:42









jezraeljezrael

340k25294365




340k25294365













  • @HookIm - You are welcome! Be free upvote solution too :)

    – jezrael
    Nov 22 '18 at 7:00






  • 1





    @pygo - Thank you.

    – jezrael
    Nov 22 '18 at 7:03






  • 1





    Nice solution +1 :-) , something new to learn

    – pygo
    Nov 22 '18 at 7:04



















  • @HookIm - You are welcome! Be free upvote solution too :)

    – jezrael
    Nov 22 '18 at 7:00






  • 1





    @pygo - Thank you.

    – jezrael
    Nov 22 '18 at 7:03






  • 1





    Nice solution +1 :-) , something new to learn

    – pygo
    Nov 22 '18 at 7:04

















@HookIm - You are welcome! Be free upvote solution too :)

– jezrael
Nov 22 '18 at 7:00





@HookIm - You are welcome! Be free upvote solution too :)

– jezrael
Nov 22 '18 at 7:00




1




1





@pygo - Thank you.

– jezrael
Nov 22 '18 at 7:03





@pygo - Thank you.

– jezrael
Nov 22 '18 at 7:03




1




1





Nice solution +1 :-) , something new to learn

– pygo
Nov 22 '18 at 7:04





Nice solution +1 :-) , something new to learn

– pygo
Nov 22 '18 at 7:04




















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%2f53425167%2fis-it-possible-to-split-column-from-specific-element-in-list%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith