0

I need to create a new data frame only using certain rows, which the value of a specific column meets a condition.

I have the following data frame (called: result):

source  target weight
     0       0   0.00
     5       1   5.00
     6       2   7.00
     8       3   8.00
     1       4   0.00
     3       5   0.00
     6       6   4.00
     4       7   0.00

and I need to get something like this:

source  target weight
     5       1   5.00
     6       2   7.00
     8       3   8.00
     6       6   4.00

However, I've been using the following procedures but they don't work and I'm getting the same result (first one) over and over.

result1 = result[result['weight'] > 0 ]

or

result1 = result.loc[result['weight'] > 0 ]
1
  • 3
    what are the dtypes here? edit your question with the output from df.info() Commented May 19, 2016 at 19:24

2 Answers 2

1

Credit @EdChum

Most certainly, your weight columns is str and in that comparison all your strings are > 0. Turn them to float, int, or something numeric and problem solved.

Hightlight (do this)

result1 = results[result['weight'].astype(float) > 0]

Explanation

Setup 1 (Problem)

text = """source  target weight
     0       0   0.00
     5       1   5.00
     6       2   7.00
     8       3   8.00
     1       4   0.00
     3       5   0.00
     6       6   4.00
     4       7   0.00"""

df = pd.read_csv(StringIO(text), delim_whitespace=1, dtype=str)
#                                                    ^^^^^^^^^
#                                               Create Problem

print df[df.weight > 0]

Looks like:

  source target weight
0      0      0   0.00
1      5      1   5.00
2      6      2   7.00
3      8      3   8.00
4      1      4   0.00
5      3      5   0.00
6      6      6   4.00
7      4      7   0.00

Setup 2 (Solution)

text = """source  target weight
     0       0   0.00
     5       1   5.00
     6       2   7.00
     8       3   8.00
     1       4   0.00
     3       5   0.00
     6       6   4.00
     4       7   0.00"""

df = pd.read_csv(StringIO(text), delim_whitespace=1, dtype=float)
#                                                    ^^^^^^^^^^^
#                                                    Fix Problem

print df[df.weight > 0]

Looks like:

   source  target  weight
1     5.0     1.0     5.0
2     6.0     2.0     7.0
3     8.0     3.0     8.0
6     6.0     6.0     4.0

For your situation, do this:

result1 = results[result['weight'].astype(float) > 0]
Sign up to request clarification or add additional context in comments.

Comments

0

Are you just removing zero? If so you can use:

df.loc[~(df==0).all(axis=1)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.