Skip to content
Advertisement

How to combine string from one column to another column at same index in pandas DataFrame?

I was doing a project in nlp. My input is:

JavaScript

I need output like this:

JavaScript

How can I achieve this?

Advertisement

Answer

You can use groupby+transform('max') to replace the empty cells with the letter per group as the letters have precedence over space. The rest is a simple string concatenation per column:

JavaScript

Used input:

JavaScript

NB. I considered “index” to be a column here, if this is the index you should use df.index in the groupby

Output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement