Need help looping through an excel file using python
So I am trying to analyze some data from excel using python. I have imported my data using pandas. Some of my data has words like 'NA". For these I am trying to loop over and ignore them. How can I do that? Another column has words such as "I", IV" etc. and I am trying to do if statements such as if value = 'I' then (...) but it doesnnt seem to work.
Here is the code:
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i]='I' and score[i]<8.0:
pass
if score[i] = 'NA'
pass
And I don't know how i'd load my sample data.
But Scores consists of numbers 1-10 and NA
levels contains values I-IV
python pandas
|
show 2 more comments
So I am trying to analyze some data from excel using python. I have imported my data using pandas. Some of my data has words like 'NA". For these I am trying to loop over and ignore them. How can I do that? Another column has words such as "I", IV" etc. and I am trying to do if statements such as if value = 'I' then (...) but it doesnnt seem to work.
Here is the code:
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i]='I' and score[i]<8.0:
pass
if score[i] = 'NA'
pass
And I don't know how i'd load my sample data.
But Scores consists of numbers 1-10 and NA
levels contains values I-IV
python pandas
1
Please post some example code, data, and expected output. Also look intodf.dropna
anddf.fillna
– Peter Leimbigler
Nov 24 '18 at 16:50
import numpy as np import pandas as pd
– CMat
Nov 24 '18 at 16:52
import numpy as np import pandas as pd data = pd.read_csv('re_data.csv') column = data['level'] score = data['scores'] for i in range(0, column1.size): if column[i]= 'I' and score[i] < 5.0: pass
– CMat
Nov 24 '18 at 16:58
So for level, I have values of I, II, III, IV
– CMat
Nov 24 '18 at 16:59
for i in range(0, column1.size): if score = 'NA': pass
– CMat
Nov 24 '18 at 17:00
|
show 2 more comments
So I am trying to analyze some data from excel using python. I have imported my data using pandas. Some of my data has words like 'NA". For these I am trying to loop over and ignore them. How can I do that? Another column has words such as "I", IV" etc. and I am trying to do if statements such as if value = 'I' then (...) but it doesnnt seem to work.
Here is the code:
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i]='I' and score[i]<8.0:
pass
if score[i] = 'NA'
pass
And I don't know how i'd load my sample data.
But Scores consists of numbers 1-10 and NA
levels contains values I-IV
python pandas
So I am trying to analyze some data from excel using python. I have imported my data using pandas. Some of my data has words like 'NA". For these I am trying to loop over and ignore them. How can I do that? Another column has words such as "I", IV" etc. and I am trying to do if statements such as if value = 'I' then (...) but it doesnnt seem to work.
Here is the code:
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i]='I' and score[i]<8.0:
pass
if score[i] = 'NA'
pass
And I don't know how i'd load my sample data.
But Scores consists of numbers 1-10 and NA
levels contains values I-IV
python pandas
python pandas
edited Nov 24 '18 at 17:16
CMat
asked Nov 24 '18 at 16:46
CMatCMat
11
11
1
Please post some example code, data, and expected output. Also look intodf.dropna
anddf.fillna
– Peter Leimbigler
Nov 24 '18 at 16:50
import numpy as np import pandas as pd
– CMat
Nov 24 '18 at 16:52
import numpy as np import pandas as pd data = pd.read_csv('re_data.csv') column = data['level'] score = data['scores'] for i in range(0, column1.size): if column[i]= 'I' and score[i] < 5.0: pass
– CMat
Nov 24 '18 at 16:58
So for level, I have values of I, II, III, IV
– CMat
Nov 24 '18 at 16:59
for i in range(0, column1.size): if score = 'NA': pass
– CMat
Nov 24 '18 at 17:00
|
show 2 more comments
1
Please post some example code, data, and expected output. Also look intodf.dropna
anddf.fillna
– Peter Leimbigler
Nov 24 '18 at 16:50
import numpy as np import pandas as pd
– CMat
Nov 24 '18 at 16:52
import numpy as np import pandas as pd data = pd.read_csv('re_data.csv') column = data['level'] score = data['scores'] for i in range(0, column1.size): if column[i]= 'I' and score[i] < 5.0: pass
– CMat
Nov 24 '18 at 16:58
So for level, I have values of I, II, III, IV
– CMat
Nov 24 '18 at 16:59
for i in range(0, column1.size): if score = 'NA': pass
– CMat
Nov 24 '18 at 17:00
1
1
Please post some example code, data, and expected output. Also look into
df.dropna
and df.fillna
– Peter Leimbigler
Nov 24 '18 at 16:50
Please post some example code, data, and expected output. Also look into
df.dropna
and df.fillna
– Peter Leimbigler
Nov 24 '18 at 16:50
import numpy as np import pandas as pd
– CMat
Nov 24 '18 at 16:52
import numpy as np import pandas as pd
– CMat
Nov 24 '18 at 16:52
import numpy as np import pandas as pd data = pd.read_csv('re_data.csv') column = data['level'] score = data['scores'] for i in range(0, column1.size): if column[i]= 'I' and score[i] < 5.0: pass
– CMat
Nov 24 '18 at 16:58
import numpy as np import pandas as pd data = pd.read_csv('re_data.csv') column = data['level'] score = data['scores'] for i in range(0, column1.size): if column[i]= 'I' and score[i] < 5.0: pass
– CMat
Nov 24 '18 at 16:58
So for level, I have values of I, II, III, IV
– CMat
Nov 24 '18 at 16:59
So for level, I have values of I, II, III, IV
– CMat
Nov 24 '18 at 16:59
for i in range(0, column1.size): if score = 'NA': pass
– CMat
Nov 24 '18 at 17:00
for i in range(0, column1.size): if score = 'NA': pass
– CMat
Nov 24 '18 at 17:00
|
show 2 more comments
1 Answer
1
active
oldest
votes
Your problem lies in two parts:
- You are using
=
instead of==
You are checking if score[i] is equal to the string 'NA'. But pandas converts 'NA' strings automatically to 'real' NA-values. So checking if it's equal to the string 'NA' wont'work.
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i] == 'I' and score[i]<8.0:
pass
elif pd.isnull(score[i]):
pass
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%2f53460299%2fneed-help-looping-through-an-excel-file-using-python%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
Your problem lies in two parts:
- You are using
=
instead of==
You are checking if score[i] is equal to the string 'NA'. But pandas converts 'NA' strings automatically to 'real' NA-values. So checking if it's equal to the string 'NA' wont'work.
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i] == 'I' and score[i]<8.0:
pass
elif pd.isnull(score[i]):
pass
add a comment |
Your problem lies in two parts:
- You are using
=
instead of==
You are checking if score[i] is equal to the string 'NA'. But pandas converts 'NA' strings automatically to 'real' NA-values. So checking if it's equal to the string 'NA' wont'work.
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i] == 'I' and score[i]<8.0:
pass
elif pd.isnull(score[i]):
pass
add a comment |
Your problem lies in two parts:
- You are using
=
instead of==
You are checking if score[i] is equal to the string 'NA'. But pandas converts 'NA' strings automatically to 'real' NA-values. So checking if it's equal to the string 'NA' wont'work.
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i] == 'I' and score[i]<8.0:
pass
elif pd.isnull(score[i]):
pass
Your problem lies in two parts:
- You are using
=
instead of==
You are checking if score[i] is equal to the string 'NA'. But pandas converts 'NA' strings automatically to 'real' NA-values. So checking if it's equal to the string 'NA' wont'work.
import numpy as np
import pandas as pd
data = pd.read_csv('re_data.csv')
column = data['level']
score = data['scores']
for i in range(0, column.size):
if column[i] == 'I' and score[i]<8.0:
pass
elif pd.isnull(score[i]):
pass
edited Nov 24 '18 at 20:33
answered Nov 24 '18 at 20:17
Niels HenkensNiels Henkens
699517
699517
add a comment |
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%2f53460299%2fneed-help-looping-through-an-excel-file-using-python%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
1
Please post some example code, data, and expected output. Also look into
df.dropna
anddf.fillna
– Peter Leimbigler
Nov 24 '18 at 16:50
import numpy as np import pandas as pd
– CMat
Nov 24 '18 at 16:52
import numpy as np import pandas as pd data = pd.read_csv('re_data.csv') column = data['level'] score = data['scores'] for i in range(0, column1.size): if column[i]= 'I' and score[i] < 5.0: pass
– CMat
Nov 24 '18 at 16:58
So for level, I have values of I, II, III, IV
– CMat
Nov 24 '18 at 16:59
for i in range(0, column1.size): if score = 'NA': pass
– CMat
Nov 24 '18 at 17:00