2020年12月20日星期日

Convert Rows to Columns and Forward Fill First Column

I have the following dataframe which has a unique URL in the first column, followed by a random number of unique keywords. I would like to transpose the keywords into a single row and forward fill the url as per my desired output below.

0                              1          2          3          4          5        www.example.com/page1         car        dog        cat        bat        apple  www.example.com/page2         dog        car   www.example.com/page3         cat        bat        apple  

Desired Output:

0                              1          2          3          4          5     www.example.com/page1         car         www.example.com/page1         dog  www.example.com/page1         cat  www.example.com/page1         bat  www.example.com/page1         apple  www.example.com/page2         dog  www.example.com/page2         car  www.example.com/page3         cat  www.example.com/page3         bat  www.example.com/page3         apple  

what I've tried: I've looked at these solutiosn, but neither seem to work for me. I've also consulted the docs on pd.melt but could not get it to work. (Still learning. Any help appreciated). Pandas create new date rows and forward fill column values Converting rows to columns using UNPIVOT

https://stackoverflow.com/questions/65385770/convert-rows-to-columns-and-forward-fill-first-column December 21, 2020 at 07:17AM

没有评论:

发表评论