pandas 数据框-将行从一个数据框移至另一数据框 [英] Pandas dataframe - move rows from one dataframe to another

查看:61
本文介绍了 pandas 数据框-将行从一个数据框移至另一数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个其中有3行的python pandas数据框:

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

我试图遍历此数据框,如果计数大于5,则必须将该行填充到新的数据框.我知道一个函数的计数是2,因为只有2行大于5.我尝试了下面的代码,但是它不起作用.任何帮助将不胜感激.

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

我尝试了这段代码-res为空,我将根据条件将行追加到res.

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

推荐答案

您可以使用 boolean indexing ,如Edchum在 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆