1

I have a python pandas dataframe that has 3 rows in it:

Name  Time  count
AAA   5:45  5
BBB   13:01 8
CCC   11:16 3

I am trying to loop through this dataframe and if the count is greater than 5, i have to populate that row to a new dataframe. I know the count is 2 from a function as only 2 rows are greater than 5. I tried the below code but it is not working. Any help would be appreciated.

for i in range(2):
  if(row['Occurences'] >= 5 ):
    df6.loc[i] = [df4['MachineName'], df4['DateTime'], df4['Count']]
df6

I tried this code - res is empty, I am appending the rows to res based on the condition.

res = pd.DataFrame(columns=('MachineName', 'DateTime', 'Occurences'))
print(pd.concat([res, df4[df4['Occurences'] >= 5]]))
res
1
  • 3
    Sorry are you just after df4[df4['Count'] >= 5]? Commented Jun 9, 2016 at 13:14

1 Answer 1

2

You can use boolean indexing only, as Edchum mentioned in comment:

import pandas as pd

df4 = pd.DataFrame({'Name': {0: 'AAA', 1: 'BBB', 2: 'CCC'}, 
                    'Time': {0: '5:45', 1: '13:01', 2: '11:16'}, 
                    'count': {0: 5, 1: 8, 2: 3}})
print (df4)
  Name   Time  count
0  AAA   5:45      5
1  BBB  13:01      8
2  CCC  11:16      3

res = pd.DataFrame(columns=('MachineName', 'DateTime', 'Occurences'))
res = pd.concat([res, df4[df4['count'] >= 5]])
print (res)
  DateTime MachineName Name Occurences   Time  count
0      NaN         NaN  AAA        NaN   5:45    5.0
1      NaN         NaN  BBB        NaN  13:01    8.0
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I have edited the question my output constraints. I am not able to print the resultant dataset to which we append the rows.
Why do you cannot write? What kind of error? Are column names same in df and df4?
I have edited the question, 'res' returns empty dataframe.
I edit answer too, first I think you need append rows to existing DataFrame.
Sorry, I think you forget res = - get output of concat to res.
|

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.