简体   繁体   English

通过迭代在Pandas DataFrame中合并行

[英]Combining rows in pandas DataFrame by iterating

How can I achieve the expected result from the following DataFrame 如何从以下DataFrame中获得预期的结果

 df
            col_1             col_2    col_3
     0  Non-Saved    www.google.com   20,567
     1             www.facebook.com      
     2             www.linkedin.com      
     3      Saved     www.Quora.com    6,337
     4                www.gmail.com      

Expected result: 预期结果:

            col_1              col_2    col_3
     0  Non-Saved     www.google.com   20,567
                    www.facebook.com
                    www.linkedin.com
     1  Saved          www.Quora.com    6,337
                       www.gmail.com   

From 5 rows to 2 rows by merging the empty strings in col_1 and col_3. 通过合并col_1和col_3中的空字符串,从5行到2行。 Also, concatenating values in col_2 into one cell. 另外,将col_2中的值串联到一个单元格中。 Can anyone help me with an user-defined function to do this? 谁能通过用户定义的功能帮助我做到这一点?

Let's try: 我们试试吧:

df = df.apply(lambda x: x.str.strip()).replace('',np.nan)

df.groupby(df.col_1.ffill())\
  .agg({'col_2': lambda x: ' '.join(x) ,'col_3':'first'})\
  .reset_index()

Output: 输出:

       col_1                                             col_2   col_3
0  Non-Saved  www.google.com www.facebook.com www.linkedin.com  20,567
1      Saved                       www.Quora.com www.gmail.com   6,337

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM