I have a data frame where the error has crept, where data composed of two different words was split into two separate cells. Example:frame where the error has crept. Example:
col1 col2 col3 col4 col5
0 A 1 2 3
1 B C 3 4 5
2 D 6 7 8
3 B E 9 10 11
I would like connect the letters from cells from the same line combine into one cell, while simultaneously moving all the rest of the row to the left. To get the result in the form:
col1 col2 col3 col4 col5
0 A 1 2 3
1 B C 3 4 5
2 D 6 7 8
3 B E 9 10 11
I'm using now df.replace(['C', 'E'], [np.nan, np.nan], regex=True)
, and later df.iloc[[n]].dropna(axis=1, how="any")
by the columns. But this is not ideal, because in the example above I get:
col1 col2 col3 col4 col5
0 A 1 2 3
1 B 3 4 5
2 D 6 7 8
3 B 9 10 11
So two lines of the same name, which in fact is not true. Because they should be called 'B C' and 'B E', respectively. In addition, each time I have to manually enter the cell names to switch to NaN, depending on the data. Do you have any some ideas?