others - python - 如何連接 Pandas dataframe中的特定行?

120 1

我想連接Pandas dataframe中的特定行。

例如:輸入:

Text= ["Abc ","def" ,"ghi ","jkl" ,"mno ","pqr" ,"stu" ]

Name= ["a ","b" ,"c ", "" ," ","f" ,"g" ]

預期輸出:

Text= ["Abc ","def" ,"ghijklmno ","pqr" ,"stu" ]

name = ["a ","b" ,"c ","f" ,"g" ]

时间: 原作者:

94 4

NaN和向前填充替換空字元串,然後groupby名稱列和聚合。


df.Name = df.Name.str.replace('', pd.np.nan).ffill()


out_df = df.groupby('Name').agg({'Text': ' '.join})



原作者:
...