则将其附加到上面的行

则将其附加到上面的行

本文介绍了如果数据框中的行以关键字开头,则将其附加到上面的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于 这里 但我没能突破它.我有一个这样结构的数据框:

I have a question similar to here but I have not been able to break through it.I have a dataframe structured like this:

0 inner join xx
1 on xx
2 and xx
3 and yy
4 and aa
5 inner join zz

我试图将以and"开头的行附加到前一行,结果如下所示:

I am trying to append the rows that start with 'and' to the previous row, resulting in something that looks like this:

0 inner join xx
1 on xx and xx and yy and aa
2 inner join zz

稍后,我将使用 'on' 关键字做同样的事情.

Later, I will then do the same thing with the 'on' keyword.

这是我到目前为止的代码.它有效,但只附加一次.给我留下一个额外的and"关键字:

This is the code that I have so far. It works, but only appends it one time. Leaving me with an extra 'and' keyword:

for row in df:
     s = df['join'].shift(-1)
     m = s.str.startswith('and', na=False)
     df.loc[m, 'join'] += (' ' + s[m])

感谢大家的专业知识,感谢您的时间.

Thank you all for your expertise and I appreciate your time.

推荐答案

可以使用groupby+apply:

(df.groupby((~df['join'].str.startswith('and ')).cumsum())
   ['join'].apply(' '.join)
)

输出:

join
1                 inner join xx
2    on xx and xx and yy and aa
3                 inner join zz
Name: join, dtype: object

这篇关于如果数据框中的行以关键字开头,则将其附加到上面的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:59