filter file by rows with python
I have a file with several columns:
1.000 2.0072 1.000 3.5368 1.000 3.7748
2.000 2.0890 2.000 3.5867 2.000 3.7302
3.000 2.0784 3.000 3.3057 3.000 3.6899
I need to filter it in such a way that I only select the rows that meet a specific condition for each column, for example just select the rows for which:
C1 < 1
C2 > 2
C3 == 3
In total there are 5 columns and I need to filter only the rows that meet the three conditions of the example. I had raised something like that but I filtered all the data at the time:
k = open ("test.txt", "r")
lines = k.readlines ()
for line in lines:
conv_int = int (lines):
if lines> 1:
print (conv_int)
python filter
add a comment |
I have a file with several columns:
1.000 2.0072 1.000 3.5368 1.000 3.7748
2.000 2.0890 2.000 3.5867 2.000 3.7302
3.000 2.0784 3.000 3.3057 3.000 3.6899
I need to filter it in such a way that I only select the rows that meet a specific condition for each column, for example just select the rows for which:
C1 < 1
C2 > 2
C3 == 3
In total there are 5 columns and I need to filter only the rows that meet the three conditions of the example. I had raised something like that but I filtered all the data at the time:
k = open ("test.txt", "r")
lines = k.readlines ()
for line in lines:
conv_int = int (lines):
if lines> 1:
print (conv_int)
python filter
2
Can you add your initial try? So people can see what went wrong instead of solving for you. It's customary on SO to show your efforts.
– ShlomiF
Nov 25 '18 at 19:31
You could consider using the pandas module and load your file as a csv (with a space separator). You would then only need to apply filters corresponding to your needs.
– ma3oun
Nov 25 '18 at 19:40
Hello, I tried to do it with pandas but I have not succeeded, I changed my data to a .csv file and did the following:
– Andres Ballesteros
Nov 26 '18 at 5:01
add a comment |
I have a file with several columns:
1.000 2.0072 1.000 3.5368 1.000 3.7748
2.000 2.0890 2.000 3.5867 2.000 3.7302
3.000 2.0784 3.000 3.3057 3.000 3.6899
I need to filter it in such a way that I only select the rows that meet a specific condition for each column, for example just select the rows for which:
C1 < 1
C2 > 2
C3 == 3
In total there are 5 columns and I need to filter only the rows that meet the three conditions of the example. I had raised something like that but I filtered all the data at the time:
k = open ("test.txt", "r")
lines = k.readlines ()
for line in lines:
conv_int = int (lines):
if lines> 1:
print (conv_int)
python filter
I have a file with several columns:
1.000 2.0072 1.000 3.5368 1.000 3.7748
2.000 2.0890 2.000 3.5867 2.000 3.7302
3.000 2.0784 3.000 3.3057 3.000 3.6899
I need to filter it in such a way that I only select the rows that meet a specific condition for each column, for example just select the rows for which:
C1 < 1
C2 > 2
C3 == 3
In total there are 5 columns and I need to filter only the rows that meet the three conditions of the example. I had raised something like that but I filtered all the data at the time:
k = open ("test.txt", "r")
lines = k.readlines ()
for line in lines:
conv_int = int (lines):
if lines> 1:
print (conv_int)
python filter
python filter
asked Nov 25 '18 at 19:26
Andres BallesterosAndres Ballesteros
1
1
2
Can you add your initial try? So people can see what went wrong instead of solving for you. It's customary on SO to show your efforts.
– ShlomiF
Nov 25 '18 at 19:31
You could consider using the pandas module and load your file as a csv (with a space separator). You would then only need to apply filters corresponding to your needs.
– ma3oun
Nov 25 '18 at 19:40
Hello, I tried to do it with pandas but I have not succeeded, I changed my data to a .csv file and did the following:
– Andres Ballesteros
Nov 26 '18 at 5:01
add a comment |
2
Can you add your initial try? So people can see what went wrong instead of solving for you. It's customary on SO to show your efforts.
– ShlomiF
Nov 25 '18 at 19:31
You could consider using the pandas module and load your file as a csv (with a space separator). You would then only need to apply filters corresponding to your needs.
– ma3oun
Nov 25 '18 at 19:40
Hello, I tried to do it with pandas but I have not succeeded, I changed my data to a .csv file and did the following:
– Andres Ballesteros
Nov 26 '18 at 5:01
2
2
Can you add your initial try? So people can see what went wrong instead of solving for you. It's customary on SO to show your efforts.
– ShlomiF
Nov 25 '18 at 19:31
Can you add your initial try? So people can see what went wrong instead of solving for you. It's customary on SO to show your efforts.
– ShlomiF
Nov 25 '18 at 19:31
You could consider using the pandas module and load your file as a csv (with a space separator). You would then only need to apply filters corresponding to your needs.
– ma3oun
Nov 25 '18 at 19:40
You could consider using the pandas module and load your file as a csv (with a space separator). You would then only need to apply filters corresponding to your needs.
– ma3oun
Nov 25 '18 at 19:40
Hello, I tried to do it with pandas but I have not succeeded, I changed my data to a .csv file and did the following:
– Andres Ballesteros
Nov 26 '18 at 5:01
Hello, I tried to do it with pandas but I have not succeeded, I changed my data to a .csv file and did the following:
– Andres Ballesteros
Nov 26 '18 at 5:01
add a comment |
1 Answer
1
active
oldest
votes
Let's get some things straight:
- I don't see an interpretation for which any of the lines in your example would pass the three filters.
- trying to cast the lines to
int
wouldn't work the way you wrote it, and is not necessarily a good idea anyway, due to the rounding effect.
But by adding some guess work, you can bend the following to your needs, hopefully getting the principles and fixing things up to you more general case:
k = open ("test.txt", "r")
lines = k.readlines()
for line in lines:
nums_list = [float(x) for x in line.strip().split(' ') if x != '']
if nums_list[0] < 1 and nums_list[1] > 2 and nums_list[2] == 3:
print(line)
Again, this won't print anything with your example, but the idea should be clear. If not then ask in the comments and I'll clarify if needed.
Good luck!
Did my suggestion not work? If so where did it fail?
– ShlomiF
Nov 26 '18 at 5:03
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%2f53471056%2ffilter-file-by-rows-with-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
Let's get some things straight:
- I don't see an interpretation for which any of the lines in your example would pass the three filters.
- trying to cast the lines to
int
wouldn't work the way you wrote it, and is not necessarily a good idea anyway, due to the rounding effect.
But by adding some guess work, you can bend the following to your needs, hopefully getting the principles and fixing things up to you more general case:
k = open ("test.txt", "r")
lines = k.readlines()
for line in lines:
nums_list = [float(x) for x in line.strip().split(' ') if x != '']
if nums_list[0] < 1 and nums_list[1] > 2 and nums_list[2] == 3:
print(line)
Again, this won't print anything with your example, but the idea should be clear. If not then ask in the comments and I'll clarify if needed.
Good luck!
Did my suggestion not work? If so where did it fail?
– ShlomiF
Nov 26 '18 at 5:03
add a comment |
Let's get some things straight:
- I don't see an interpretation for which any of the lines in your example would pass the three filters.
- trying to cast the lines to
int
wouldn't work the way you wrote it, and is not necessarily a good idea anyway, due to the rounding effect.
But by adding some guess work, you can bend the following to your needs, hopefully getting the principles and fixing things up to you more general case:
k = open ("test.txt", "r")
lines = k.readlines()
for line in lines:
nums_list = [float(x) for x in line.strip().split(' ') if x != '']
if nums_list[0] < 1 and nums_list[1] > 2 and nums_list[2] == 3:
print(line)
Again, this won't print anything with your example, but the idea should be clear. If not then ask in the comments and I'll clarify if needed.
Good luck!
Did my suggestion not work? If so where did it fail?
– ShlomiF
Nov 26 '18 at 5:03
add a comment |
Let's get some things straight:
- I don't see an interpretation for which any of the lines in your example would pass the three filters.
- trying to cast the lines to
int
wouldn't work the way you wrote it, and is not necessarily a good idea anyway, due to the rounding effect.
But by adding some guess work, you can bend the following to your needs, hopefully getting the principles and fixing things up to you more general case:
k = open ("test.txt", "r")
lines = k.readlines()
for line in lines:
nums_list = [float(x) for x in line.strip().split(' ') if x != '']
if nums_list[0] < 1 and nums_list[1] > 2 and nums_list[2] == 3:
print(line)
Again, this won't print anything with your example, but the idea should be clear. If not then ask in the comments and I'll clarify if needed.
Good luck!
Let's get some things straight:
- I don't see an interpretation for which any of the lines in your example would pass the three filters.
- trying to cast the lines to
int
wouldn't work the way you wrote it, and is not necessarily a good idea anyway, due to the rounding effect.
But by adding some guess work, you can bend the following to your needs, hopefully getting the principles and fixing things up to you more general case:
k = open ("test.txt", "r")
lines = k.readlines()
for line in lines:
nums_list = [float(x) for x in line.strip().split(' ') if x != '']
if nums_list[0] < 1 and nums_list[1] > 2 and nums_list[2] == 3:
print(line)
Again, this won't print anything with your example, but the idea should be clear. If not then ask in the comments and I'll clarify if needed.
Good luck!
answered Nov 25 '18 at 19:52
ShlomiFShlomiF
845410
845410
Did my suggestion not work? If so where did it fail?
– ShlomiF
Nov 26 '18 at 5:03
add a comment |
Did my suggestion not work? If so where did it fail?
– ShlomiF
Nov 26 '18 at 5:03
Did my suggestion not work? If so where did it fail?
– ShlomiF
Nov 26 '18 at 5:03
Did my suggestion not work? If so where did it fail?
– ShlomiF
Nov 26 '18 at 5:03
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%2f53471056%2ffilter-file-by-rows-with-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
2
Can you add your initial try? So people can see what went wrong instead of solving for you. It's customary on SO to show your efforts.
– ShlomiF
Nov 25 '18 at 19:31
You could consider using the pandas module and load your file as a csv (with a space separator). You would then only need to apply filters corresponding to your needs.
– ma3oun
Nov 25 '18 at 19:40
Hello, I tried to do it with pandas but I have not succeeded, I changed my data to a .csv file and did the following:
– Andres Ballesteros
Nov 26 '18 at 5:01