is python possible to create a new data frame from the existing data frame?












0














i would like to ask about this because i have created a code like this but it returned only the header.



Here is the sample of my df name df_all



Doc       name count year
[A1,A2] John 1 2018
[A1,A3] Mark 0 2018
[A2,A4] John 3 2018


here is the code that i have tried



n_wsp_71 = [i for i in df_all if i.count != 0]
n_wsp_71


and here is my results



['Doc', 'name', 'count', 'year']


but i expect to see this results



newdf
Doc name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018









share|improve this question




















  • 2




    That's a dataframe not a list...
    – U9-Forward
    Nov 23 '18 at 5:14










  • Try list_all = list_all[list_all['count'] != 0]
    – Sociopath
    Nov 23 '18 at 5:16










  • im blur, sorry. i have updated the post
    – Hook Im
    Nov 23 '18 at 5:17
















0














i would like to ask about this because i have created a code like this but it returned only the header.



Here is the sample of my df name df_all



Doc       name count year
[A1,A2] John 1 2018
[A1,A3] Mark 0 2018
[A2,A4] John 3 2018


here is the code that i have tried



n_wsp_71 = [i for i in df_all if i.count != 0]
n_wsp_71


and here is my results



['Doc', 'name', 'count', 'year']


but i expect to see this results



newdf
Doc name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018









share|improve this question




















  • 2




    That's a dataframe not a list...
    – U9-Forward
    Nov 23 '18 at 5:14










  • Try list_all = list_all[list_all['count'] != 0]
    – Sociopath
    Nov 23 '18 at 5:16










  • im blur, sorry. i have updated the post
    – Hook Im
    Nov 23 '18 at 5:17














0












0








0







i would like to ask about this because i have created a code like this but it returned only the header.



Here is the sample of my df name df_all



Doc       name count year
[A1,A2] John 1 2018
[A1,A3] Mark 0 2018
[A2,A4] John 3 2018


here is the code that i have tried



n_wsp_71 = [i for i in df_all if i.count != 0]
n_wsp_71


and here is my results



['Doc', 'name', 'count', 'year']


but i expect to see this results



newdf
Doc name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018









share|improve this question















i would like to ask about this because i have created a code like this but it returned only the header.



Here is the sample of my df name df_all



Doc       name count year
[A1,A2] John 1 2018
[A1,A3] Mark 0 2018
[A2,A4] John 3 2018


here is the code that i have tried



n_wsp_71 = [i for i in df_all if i.count != 0]
n_wsp_71


and here is my results



['Doc', 'name', 'count', 'year']


but i expect to see this results



newdf
Doc name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018






python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:15

























asked Nov 23 '18 at 5:12









Hook Im

1248




1248








  • 2




    That's a dataframe not a list...
    – U9-Forward
    Nov 23 '18 at 5:14










  • Try list_all = list_all[list_all['count'] != 0]
    – Sociopath
    Nov 23 '18 at 5:16










  • im blur, sorry. i have updated the post
    – Hook Im
    Nov 23 '18 at 5:17














  • 2




    That's a dataframe not a list...
    – U9-Forward
    Nov 23 '18 at 5:14










  • Try list_all = list_all[list_all['count'] != 0]
    – Sociopath
    Nov 23 '18 at 5:16










  • im blur, sorry. i have updated the post
    – Hook Im
    Nov 23 '18 at 5:17








2




2




That's a dataframe not a list...
– U9-Forward
Nov 23 '18 at 5:14




That's a dataframe not a list...
– U9-Forward
Nov 23 '18 at 5:14












Try list_all = list_all[list_all['count'] != 0]
– Sociopath
Nov 23 '18 at 5:16




Try list_all = list_all[list_all['count'] != 0]
– Sociopath
Nov 23 '18 at 5:16












im blur, sorry. i have updated the post
– Hook Im
Nov 23 '18 at 5:17




im blur, sorry. i have updated the post
– Hook Im
Nov 23 '18 at 5:17












2 Answers
2






active

oldest

votes


















1














You're doing a list comprehension to pandas data-frames, that will never work



So you have to do:



list_all=list_all[list_all['count']!=0]


And now:



print(list_all)


Is:



Doc       name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018





share|improve this answer

















  • 1




    thank you for your understanding and comment!
    – Hook Im
    Nov 23 '18 at 5:28










  • @HookIm Happy to help, :-), 😃😃😃, Note: mine is faster based on the timings Chirag's: 0.04203809888741396 Mine: 0.010112490249476552
    – U9-Forward
    Nov 23 '18 at 5:34






  • 1




    ah.. i see! thank you for reminding me to consider the runtime too. by the way, i i would like to return count == 0. when i tried, it has nothing come out. why it became like that?
    – Hook Im
    Nov 23 '18 at 5:41










  • @HookIm YW, happy to help again, but why it did not work for count==0?, did you try: df[df['count']==0]
    – U9-Forward
    Nov 23 '18 at 5:43






  • 1




    yes, already did that. but, i will fix this by myself :) thank you so much!
    – Hook Im
    Nov 23 '18 at 5:50





















2














df = pd.DataFrame({"Doc": [["A1","A2"], ["A1","A3"], ["A2","A4"]], "name": ["John", "Mark", "John"], "count": [1,0,3], "year": [2018, 2018, 2018]})

df2 = df.query("count!=0").reset_index(drop=True)

# for count = 0
df2 = df.query("count==0").reset_index(drop=True)

#method 2
df2 = df[~(df["count"].isin(['0']))].reset_index(drop=True)

# for count = 0
df2 = df[(df["count"].isin(['0']))].reset_index(drop=True)
print(df2)


Output:



        Doc  name  count  year
0 [A1, A2] John 1 2018
1 [A2, A4] John 3 2018

Doc name count year
0 [A1, A3] Mark 0 2018





share|improve this answer























  • i have not considered using the query before, new thing to learn! thank you. by the way, when i try to show the count that equal to 0. why it shows nothing. is it a sensitive case?
    – Hook Im
    Nov 23 '18 at 5:30










  • @HookIm Updated the alternative method. Also for count=0
    – Srce Cde
    Nov 23 '18 at 5:56











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%2f53440942%2fis-python-possible-to-create-a-new-data-frame-from-the-existing-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









1














You're doing a list comprehension to pandas data-frames, that will never work



So you have to do:



list_all=list_all[list_all['count']!=0]


And now:



print(list_all)


Is:



Doc       name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018





share|improve this answer

















  • 1




    thank you for your understanding and comment!
    – Hook Im
    Nov 23 '18 at 5:28










  • @HookIm Happy to help, :-), 😃😃😃, Note: mine is faster based on the timings Chirag's: 0.04203809888741396 Mine: 0.010112490249476552
    – U9-Forward
    Nov 23 '18 at 5:34






  • 1




    ah.. i see! thank you for reminding me to consider the runtime too. by the way, i i would like to return count == 0. when i tried, it has nothing come out. why it became like that?
    – Hook Im
    Nov 23 '18 at 5:41










  • @HookIm YW, happy to help again, but why it did not work for count==0?, did you try: df[df['count']==0]
    – U9-Forward
    Nov 23 '18 at 5:43






  • 1




    yes, already did that. but, i will fix this by myself :) thank you so much!
    – Hook Im
    Nov 23 '18 at 5:50


















1














You're doing a list comprehension to pandas data-frames, that will never work



So you have to do:



list_all=list_all[list_all['count']!=0]


And now:



print(list_all)


Is:



Doc       name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018





share|improve this answer

















  • 1




    thank you for your understanding and comment!
    – Hook Im
    Nov 23 '18 at 5:28










  • @HookIm Happy to help, :-), 😃😃😃, Note: mine is faster based on the timings Chirag's: 0.04203809888741396 Mine: 0.010112490249476552
    – U9-Forward
    Nov 23 '18 at 5:34






  • 1




    ah.. i see! thank you for reminding me to consider the runtime too. by the way, i i would like to return count == 0. when i tried, it has nothing come out. why it became like that?
    – Hook Im
    Nov 23 '18 at 5:41










  • @HookIm YW, happy to help again, but why it did not work for count==0?, did you try: df[df['count']==0]
    – U9-Forward
    Nov 23 '18 at 5:43






  • 1




    yes, already did that. but, i will fix this by myself :) thank you so much!
    – Hook Im
    Nov 23 '18 at 5:50
















1












1








1






You're doing a list comprehension to pandas data-frames, that will never work



So you have to do:



list_all=list_all[list_all['count']!=0]


And now:



print(list_all)


Is:



Doc       name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018





share|improve this answer












You're doing a list comprehension to pandas data-frames, that will never work



So you have to do:



list_all=list_all[list_all['count']!=0]


And now:



print(list_all)


Is:



Doc       name count year
[A1,A2] John 1 2018
[A2,A4] John 3 2018






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 5:17









U9-Forward

13k21137




13k21137








  • 1




    thank you for your understanding and comment!
    – Hook Im
    Nov 23 '18 at 5:28










  • @HookIm Happy to help, :-), 😃😃😃, Note: mine is faster based on the timings Chirag's: 0.04203809888741396 Mine: 0.010112490249476552
    – U9-Forward
    Nov 23 '18 at 5:34






  • 1




    ah.. i see! thank you for reminding me to consider the runtime too. by the way, i i would like to return count == 0. when i tried, it has nothing come out. why it became like that?
    – Hook Im
    Nov 23 '18 at 5:41










  • @HookIm YW, happy to help again, but why it did not work for count==0?, did you try: df[df['count']==0]
    – U9-Forward
    Nov 23 '18 at 5:43






  • 1




    yes, already did that. but, i will fix this by myself :) thank you so much!
    – Hook Im
    Nov 23 '18 at 5:50
















  • 1




    thank you for your understanding and comment!
    – Hook Im
    Nov 23 '18 at 5:28










  • @HookIm Happy to help, :-), 😃😃😃, Note: mine is faster based on the timings Chirag's: 0.04203809888741396 Mine: 0.010112490249476552
    – U9-Forward
    Nov 23 '18 at 5:34






  • 1




    ah.. i see! thank you for reminding me to consider the runtime too. by the way, i i would like to return count == 0. when i tried, it has nothing come out. why it became like that?
    – Hook Im
    Nov 23 '18 at 5:41










  • @HookIm YW, happy to help again, but why it did not work for count==0?, did you try: df[df['count']==0]
    – U9-Forward
    Nov 23 '18 at 5:43






  • 1




    yes, already did that. but, i will fix this by myself :) thank you so much!
    – Hook Im
    Nov 23 '18 at 5:50










1




1




thank you for your understanding and comment!
– Hook Im
Nov 23 '18 at 5:28




thank you for your understanding and comment!
– Hook Im
Nov 23 '18 at 5:28












@HookIm Happy to help, :-), 😃😃😃, Note: mine is faster based on the timings Chirag's: 0.04203809888741396 Mine: 0.010112490249476552
– U9-Forward
Nov 23 '18 at 5:34




@HookIm Happy to help, :-), 😃😃😃, Note: mine is faster based on the timings Chirag's: 0.04203809888741396 Mine: 0.010112490249476552
– U9-Forward
Nov 23 '18 at 5:34




1




1




ah.. i see! thank you for reminding me to consider the runtime too. by the way, i i would like to return count == 0. when i tried, it has nothing come out. why it became like that?
– Hook Im
Nov 23 '18 at 5:41




ah.. i see! thank you for reminding me to consider the runtime too. by the way, i i would like to return count == 0. when i tried, it has nothing come out. why it became like that?
– Hook Im
Nov 23 '18 at 5:41












@HookIm YW, happy to help again, but why it did not work for count==0?, did you try: df[df['count']==0]
– U9-Forward
Nov 23 '18 at 5:43




@HookIm YW, happy to help again, but why it did not work for count==0?, did you try: df[df['count']==0]
– U9-Forward
Nov 23 '18 at 5:43




1




1




yes, already did that. but, i will fix this by myself :) thank you so much!
– Hook Im
Nov 23 '18 at 5:50






yes, already did that. but, i will fix this by myself :) thank you so much!
– Hook Im
Nov 23 '18 at 5:50















2














df = pd.DataFrame({"Doc": [["A1","A2"], ["A1","A3"], ["A2","A4"]], "name": ["John", "Mark", "John"], "count": [1,0,3], "year": [2018, 2018, 2018]})

df2 = df.query("count!=0").reset_index(drop=True)

# for count = 0
df2 = df.query("count==0").reset_index(drop=True)

#method 2
df2 = df[~(df["count"].isin(['0']))].reset_index(drop=True)

# for count = 0
df2 = df[(df["count"].isin(['0']))].reset_index(drop=True)
print(df2)


Output:



        Doc  name  count  year
0 [A1, A2] John 1 2018
1 [A2, A4] John 3 2018

Doc name count year
0 [A1, A3] Mark 0 2018





share|improve this answer























  • i have not considered using the query before, new thing to learn! thank you. by the way, when i try to show the count that equal to 0. why it shows nothing. is it a sensitive case?
    – Hook Im
    Nov 23 '18 at 5:30










  • @HookIm Updated the alternative method. Also for count=0
    – Srce Cde
    Nov 23 '18 at 5:56
















2














df = pd.DataFrame({"Doc": [["A1","A2"], ["A1","A3"], ["A2","A4"]], "name": ["John", "Mark", "John"], "count": [1,0,3], "year": [2018, 2018, 2018]})

df2 = df.query("count!=0").reset_index(drop=True)

# for count = 0
df2 = df.query("count==0").reset_index(drop=True)

#method 2
df2 = df[~(df["count"].isin(['0']))].reset_index(drop=True)

# for count = 0
df2 = df[(df["count"].isin(['0']))].reset_index(drop=True)
print(df2)


Output:



        Doc  name  count  year
0 [A1, A2] John 1 2018
1 [A2, A4] John 3 2018

Doc name count year
0 [A1, A3] Mark 0 2018





share|improve this answer























  • i have not considered using the query before, new thing to learn! thank you. by the way, when i try to show the count that equal to 0. why it shows nothing. is it a sensitive case?
    – Hook Im
    Nov 23 '18 at 5:30










  • @HookIm Updated the alternative method. Also for count=0
    – Srce Cde
    Nov 23 '18 at 5:56














2












2








2






df = pd.DataFrame({"Doc": [["A1","A2"], ["A1","A3"], ["A2","A4"]], "name": ["John", "Mark", "John"], "count": [1,0,3], "year": [2018, 2018, 2018]})

df2 = df.query("count!=0").reset_index(drop=True)

# for count = 0
df2 = df.query("count==0").reset_index(drop=True)

#method 2
df2 = df[~(df["count"].isin(['0']))].reset_index(drop=True)

# for count = 0
df2 = df[(df["count"].isin(['0']))].reset_index(drop=True)
print(df2)


Output:



        Doc  name  count  year
0 [A1, A2] John 1 2018
1 [A2, A4] John 3 2018

Doc name count year
0 [A1, A3] Mark 0 2018





share|improve this answer














df = pd.DataFrame({"Doc": [["A1","A2"], ["A1","A3"], ["A2","A4"]], "name": ["John", "Mark", "John"], "count": [1,0,3], "year": [2018, 2018, 2018]})

df2 = df.query("count!=0").reset_index(drop=True)

# for count = 0
df2 = df.query("count==0").reset_index(drop=True)

#method 2
df2 = df[~(df["count"].isin(['0']))].reset_index(drop=True)

# for count = 0
df2 = df[(df["count"].isin(['0']))].reset_index(drop=True)
print(df2)


Output:



        Doc  name  count  year
0 [A1, A2] John 1 2018
1 [A2, A4] John 3 2018

Doc name count year
0 [A1, A3] Mark 0 2018






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 6:02

























answered Nov 23 '18 at 5:26









Srce Cde

1,136511




1,136511












  • i have not considered using the query before, new thing to learn! thank you. by the way, when i try to show the count that equal to 0. why it shows nothing. is it a sensitive case?
    – Hook Im
    Nov 23 '18 at 5:30










  • @HookIm Updated the alternative method. Also for count=0
    – Srce Cde
    Nov 23 '18 at 5:56


















  • i have not considered using the query before, new thing to learn! thank you. by the way, when i try to show the count that equal to 0. why it shows nothing. is it a sensitive case?
    – Hook Im
    Nov 23 '18 at 5:30










  • @HookIm Updated the alternative method. Also for count=0
    – Srce Cde
    Nov 23 '18 at 5:56
















i have not considered using the query before, new thing to learn! thank you. by the way, when i try to show the count that equal to 0. why it shows nothing. is it a sensitive case?
– Hook Im
Nov 23 '18 at 5:30




i have not considered using the query before, new thing to learn! thank you. by the way, when i try to show the count that equal to 0. why it shows nothing. is it a sensitive case?
– Hook Im
Nov 23 '18 at 5:30












@HookIm Updated the alternative method. Also for count=0
– Srce Cde
Nov 23 '18 at 5:56




@HookIm Updated the alternative method. Also for count=0
– Srce Cde
Nov 23 '18 at 5:56


















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%2f53440942%2fis-python-possible-to-create-a-new-data-frame-from-the-existing-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

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu