Pandas dataframe - move rows from one dataframe to another

南楼画角 提交于 2021-01-29 04:04:41

问题


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:


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


来源:https://stackoverflow.com/questions/37727018/pandas-dataframe-move-rows-from-one-dataframe-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!