is it possible to split column from specific element in list?
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

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

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
add a comment |
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

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

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
One question - Is not problem mixed empty values with numeric? Not better omitdf_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
add a comment |
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

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

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

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

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
python pandas list
asked Nov 22 '18 at 6:38
Hook ImHook Im
1318
1318
One question - Is not problem mixed empty values with numeric? Not better omitdf_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
add a comment |
One question - Is not problem mixed empty values with numeric? Not better omitdf_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
add a comment |
1 Answer
1
active
oldest
votes
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)
@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
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%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
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)
@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
add a comment |
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)
@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
add a comment |
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)
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)
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
add a comment |
@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
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%2f53425167%2fis-it-possible-to-split-column-from-specific-element-in-list%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

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