Counting columns in a csv file IF column contains a 1. (only 1's and 0's in csv file) Python



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I am trying to loop through a csv file of ones and zeroes, in Python. If the column has a 1 in it then I count that column and add to the counter. I expect
a count of 3 but I'm getting 18. I can achieve correct answer when I need to count rows with ones in them but stuck with columns. Any pointers appreciated, here is what I have:



 # section 2 feature 3.
with open(file_path) as csv_file_to_read:
width_in = csv.reader(csv_file_to_read, delimiter=',')
count_col = 0
for row in width_in:
for column in row:
if '1' in column:
count_col += 1
print(count_col, ' f3')


Thank you.










share|improve this question






















  • Fire up a debugger and step through the code, or add some strategically placed print statements to check your assumptions (csv parsing, content of a cell, ...).

    – Eugen Constantin Dinca
    Nov 15 '18 at 15:55






  • 1





    Can you show us the data?

    – Owen
    Nov 15 '18 at 16:00











  • Your code will count the total number of 1s in the data. If you want to count the number of columns which contain a 1, you'll need to handle columns with 1s in multiple rows.

    – Owen
    Nov 15 '18 at 16:26

















-1















I am trying to loop through a csv file of ones and zeroes, in Python. If the column has a 1 in it then I count that column and add to the counter. I expect
a count of 3 but I'm getting 18. I can achieve correct answer when I need to count rows with ones in them but stuck with columns. Any pointers appreciated, here is what I have:



 # section 2 feature 3.
with open(file_path) as csv_file_to_read:
width_in = csv.reader(csv_file_to_read, delimiter=',')
count_col = 0
for row in width_in:
for column in row:
if '1' in column:
count_col += 1
print(count_col, ' f3')


Thank you.










share|improve this question






















  • Fire up a debugger and step through the code, or add some strategically placed print statements to check your assumptions (csv parsing, content of a cell, ...).

    – Eugen Constantin Dinca
    Nov 15 '18 at 15:55






  • 1





    Can you show us the data?

    – Owen
    Nov 15 '18 at 16:00











  • Your code will count the total number of 1s in the data. If you want to count the number of columns which contain a 1, you'll need to handle columns with 1s in multiple rows.

    – Owen
    Nov 15 '18 at 16:26













-1












-1








-1








I am trying to loop through a csv file of ones and zeroes, in Python. If the column has a 1 in it then I count that column and add to the counter. I expect
a count of 3 but I'm getting 18. I can achieve correct answer when I need to count rows with ones in them but stuck with columns. Any pointers appreciated, here is what I have:



 # section 2 feature 3.
with open(file_path) as csv_file_to_read:
width_in = csv.reader(csv_file_to_read, delimiter=',')
count_col = 0
for row in width_in:
for column in row:
if '1' in column:
count_col += 1
print(count_col, ' f3')


Thank you.










share|improve this question














I am trying to loop through a csv file of ones and zeroes, in Python. If the column has a 1 in it then I count that column and add to the counter. I expect
a count of 3 but I'm getting 18. I can achieve correct answer when I need to count rows with ones in them but stuck with columns. Any pointers appreciated, here is what I have:



 # section 2 feature 3.
with open(file_path) as csv_file_to_read:
width_in = csv.reader(csv_file_to_read, delimiter=',')
count_col = 0
for row in width_in:
for column in row:
if '1' in column:
count_col += 1
print(count_col, ' f3')


Thank you.







python csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 15:50









wantToCodewantToCode

204




204












  • Fire up a debugger and step through the code, or add some strategically placed print statements to check your assumptions (csv parsing, content of a cell, ...).

    – Eugen Constantin Dinca
    Nov 15 '18 at 15:55






  • 1





    Can you show us the data?

    – Owen
    Nov 15 '18 at 16:00











  • Your code will count the total number of 1s in the data. If you want to count the number of columns which contain a 1, you'll need to handle columns with 1s in multiple rows.

    – Owen
    Nov 15 '18 at 16:26

















  • Fire up a debugger and step through the code, or add some strategically placed print statements to check your assumptions (csv parsing, content of a cell, ...).

    – Eugen Constantin Dinca
    Nov 15 '18 at 15:55






  • 1





    Can you show us the data?

    – Owen
    Nov 15 '18 at 16:00











  • Your code will count the total number of 1s in the data. If you want to count the number of columns which contain a 1, you'll need to handle columns with 1s in multiple rows.

    – Owen
    Nov 15 '18 at 16:26
















Fire up a debugger and step through the code, or add some strategically placed print statements to check your assumptions (csv parsing, content of a cell, ...).

– Eugen Constantin Dinca
Nov 15 '18 at 15:55





Fire up a debugger and step through the code, or add some strategically placed print statements to check your assumptions (csv parsing, content of a cell, ...).

– Eugen Constantin Dinca
Nov 15 '18 at 15:55




1




1





Can you show us the data?

– Owen
Nov 15 '18 at 16:00





Can you show us the data?

– Owen
Nov 15 '18 at 16:00













Your code will count the total number of 1s in the data. If you want to count the number of columns which contain a 1, you'll need to handle columns with 1s in multiple rows.

– Owen
Nov 15 '18 at 16:26





Your code will count the total number of 1s in the data. If you want to count the number of columns which contain a 1, you'll need to handle columns with 1s in multiple rows.

– Owen
Nov 15 '18 at 16:26












1 Answer
1






active

oldest

votes


















0














Using .read_csv()



import pandas as pd

df = pd.read_csv("file.csv")
count_col = 0
for i in df.columns:
if any(df[i].isin([1])):
count_col += 1
print(count_col)


Hope this helps. Since, you have not shown the data, it is based on the assumption.






share|improve this answer

























  • Thank you Srce Cde. Perfect. Gave me the value I was looking for. Have not used pandas before.

    – wantToCode
    Nov 15 '18 at 17:38











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%2f53323137%2fcounting-columns-in-a-csv-file-if-column-contains-a-1-only-1s-and-0s-in-csv%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









0














Using .read_csv()



import pandas as pd

df = pd.read_csv("file.csv")
count_col = 0
for i in df.columns:
if any(df[i].isin([1])):
count_col += 1
print(count_col)


Hope this helps. Since, you have not shown the data, it is based on the assumption.






share|improve this answer

























  • Thank you Srce Cde. Perfect. Gave me the value I was looking for. Have not used pandas before.

    – wantToCode
    Nov 15 '18 at 17:38















0














Using .read_csv()



import pandas as pd

df = pd.read_csv("file.csv")
count_col = 0
for i in df.columns:
if any(df[i].isin([1])):
count_col += 1
print(count_col)


Hope this helps. Since, you have not shown the data, it is based on the assumption.






share|improve this answer

























  • Thank you Srce Cde. Perfect. Gave me the value I was looking for. Have not used pandas before.

    – wantToCode
    Nov 15 '18 at 17:38













0












0








0







Using .read_csv()



import pandas as pd

df = pd.read_csv("file.csv")
count_col = 0
for i in df.columns:
if any(df[i].isin([1])):
count_col += 1
print(count_col)


Hope this helps. Since, you have not shown the data, it is based on the assumption.






share|improve this answer















Using .read_csv()



import pandas as pd

df = pd.read_csv("file.csv")
count_col = 0
for i in df.columns:
if any(df[i].isin([1])):
count_col += 1
print(count_col)


Hope this helps. Since, you have not shown the data, it is based on the assumption.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 18:13

























answered Nov 15 '18 at 16:31









Srce CdeSrce Cde

1,194612




1,194612












  • Thank you Srce Cde. Perfect. Gave me the value I was looking for. Have not used pandas before.

    – wantToCode
    Nov 15 '18 at 17:38

















  • Thank you Srce Cde. Perfect. Gave me the value I was looking for. Have not used pandas before.

    – wantToCode
    Nov 15 '18 at 17:38
















Thank you Srce Cde. Perfect. Gave me the value I was looking for. Have not used pandas before.

– wantToCode
Nov 15 '18 at 17:38





Thank you Srce Cde. Perfect. Gave me the value I was looking for. Have not used pandas before.

– wantToCode
Nov 15 '18 at 17:38



















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%2f53323137%2fcounting-columns-in-a-csv-file-if-column-contains-a-1-only-1s-and-0s-in-csv%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

Darth Vader #20

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

Ondo