user3447653
user3447653

Reputation: 4158

Pandas dataframe - move rows from one dataframe to another

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

Upvotes: 1

Views: 4918

Answers (1)

jezrael
jezrael

Reputation: 862671

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

Upvotes: 2

Related Questions