Reputation: 6197
If rows of a column are empty or whitespace strings, I want to fill them with the corresponding row values of a particular column.
Here is a minimal example,
Starting dataframe
d1 = [['a', 'b', '3', 'd', '8', '9'], ['q', '', 'j', ' ', 'g', '\n']]
dtest = pd.DataFrame(d1).transpose()
dtest
0 1
0 a q
1 b
2 3 j
3 d
4 8 g
5 9 \n
And I am stuck trying to figure out how to create a dataframe which would result in the following.
result = [['a', 'b', '3', 'd', '8', '9'], ['q', 'b', 'j', 'd', 'g', '9']]
resultdf = pd.DataFrame(result).transpose()
resultdf
0 1
0 a q
1 b b
2 3 j
3 d d
4 8 g
5 9 9
Upvotes: 1
Views: 59
Reputation: 150745
A combination of strip
and where
:
dtest[1] = dtest[1].where(dtest[1].str.strip().ne(''), dtest[0])
Output:
0 1
0 a q
1 b b
2 3 j
3 d d
4 8 g
5 9 9
Upvotes: 1